Files
upstream-gitea/services/pubsub/redis_test.go
upstream-mirror 52ae0c5802
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:41:59 +05:00

74 lines
2.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pubsub
import (
"testing"
"time"
"gitea.dev/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestRedisBroker runs the shared broker scenarios against a real Redis-backed
// RedisBroker, plus the backend-specific ones. Delivery crosses a Redis
// round-trip (hence the wider timeout) and HasTopicSubscribers answers
// conservatively true, so subscriber tracking is not exact.
// One redis-server is shared by all of them; starting one per test dominated
// the package runtime.
func TestRedisBroker(t *testing.T) {
redisConn := test.PrepareTestRedis(t)
newBroker := func(t *testing.T) Broker {
broker, err := NewRedisBroker(redisConn)
require.NoError(t, err)
return broker
}
testBrokerBasic(t, newBroker, 2*time.Second)
// RedisBroker cannot see other processes' subscribers, so it answers true even
// with none locally rather than risk dropping a push.
t.Run("HasTopicSubscribersWithoutAny", func(t *testing.T) {
assert.True(t, newBroker(t).HasTopicSubscribers("topic"))
})
// RedisBroker tears down its per-topic Redis subscription and internal
// state once the last local subscriber cancels.
t.Run("CancelCleansTopicState", func(t *testing.T) {
b, isRedisBroker := newBroker(t).(*RedisBroker)
require.True(t, isRedisBroker)
ch, cancel := b.Subscribe(t.Name())
cancel()
_, ok := <-ch
assert.False(t, ok, "channel must be closed after cancel")
b.mu.RLock()
_, present := b.topics[t.Name()]
b.mu.RUnlock()
assert.False(t, present, "topic state must be removed after last subscriber cancels")
})
// Two RedisBroker instances sharing one Redis simulate two Gitea processes -
// a publish on one must reach a subscriber on the other.
t.Run("CrossBroker", func(t *testing.T) {
publisher, subscriber := newBroker(t), newBroker(t)
ch, cancel := subscriber.Subscribe(t.Name())
defer cancel()
waitSubscribed(t, subscriber, t.Name())
publisher.Publish(t.Name(), []byte("cross-process"))
select {
case msg := <-ch:
assert.Equal(t, []byte("cross-process"), msg)
case <-time.After(2 * time.Second):
t.Fatal("subscriber on second broker did not receive message")
}
})
}