2023-02-27 19:07:06 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-05-02 16:10:27 +00:00
|
|
|
"math/rand"
|
2023-02-27 19:07:06 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
2023-03-04 03:22:07 +00:00
|
|
|
"time"
|
2023-05-02 16:10:27 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-27 19:07:06 +00:00
|
|
|
)
|
|
|
|
|
2023-05-13 18:39:31 +00:00
|
|
|
func TestTopic_CancelSubscribersExceptUser(t *testing.T) {
|
2023-02-27 19:07:06 +00:00
|
|
|
subFn := func(v *visitor, msg *message) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
canceled1 := atomic.Bool{}
|
|
|
|
cancelFn1 := func() {
|
|
|
|
canceled1.Store(true)
|
|
|
|
}
|
|
|
|
canceled2 := atomic.Bool{}
|
|
|
|
cancelFn2 := func() {
|
|
|
|
canceled2.Store(true)
|
|
|
|
}
|
|
|
|
to := newTopic("mytopic")
|
|
|
|
to.Subscribe(subFn, "", cancelFn1)
|
|
|
|
to.Subscribe(subFn, "u_phil", cancelFn2)
|
|
|
|
|
2023-05-13 18:39:31 +00:00
|
|
|
to.CancelSubscribersExceptUser("u_phil")
|
2023-02-27 19:07:06 +00:00
|
|
|
require.True(t, canceled1.Load())
|
|
|
|
require.False(t, canceled2.Load())
|
|
|
|
}
|
2023-03-04 03:22:07 +00:00
|
|
|
|
2023-05-13 18:39:31 +00:00
|
|
|
func TestTopic_CancelSubscribersUser(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
subFn := func(v *visitor, msg *message) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
canceled1 := atomic.Bool{}
|
|
|
|
cancelFn1 := func() {
|
|
|
|
canceled1.Store(true)
|
|
|
|
}
|
|
|
|
canceled2 := atomic.Bool{}
|
|
|
|
cancelFn2 := func() {
|
|
|
|
canceled2.Store(true)
|
|
|
|
}
|
|
|
|
to := newTopic("mytopic")
|
|
|
|
to.Subscribe(subFn, "u_another", cancelFn1)
|
|
|
|
to.Subscribe(subFn, "u_phil", cancelFn2)
|
|
|
|
|
|
|
|
to.CancelSubscriberUser("u_phil")
|
|
|
|
require.False(t, canceled1.Load())
|
|
|
|
require.True(t, canceled2.Load())
|
|
|
|
}
|
|
|
|
|
2023-03-04 03:22:07 +00:00
|
|
|
func TestTopic_Keepalive(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
to := newTopic("mytopic")
|
|
|
|
to.lastAccess = time.Now().Add(-1 * time.Hour)
|
|
|
|
to.Keepalive()
|
|
|
|
require.True(t, to.LastAccess().Unix() >= time.Now().Unix()-2)
|
|
|
|
require.True(t, to.LastAccess().Unix() <= time.Now().Unix()+2)
|
|
|
|
}
|
2023-05-02 16:10:27 +00:00
|
|
|
|
2023-05-02 18:16:59 +00:00
|
|
|
func TestTopic_Subscribe_DuplicateID(t *testing.T) {
|
2023-05-02 16:10:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
to := newTopic("mytopic")
|
|
|
|
|
2023-11-17 02:04:49 +00:00
|
|
|
//lint:ignore SA1019 Fix random seed to force same number generation
|
2023-05-02 16:10:27 +00:00
|
|
|
rand.Seed(1)
|
|
|
|
a := rand.Int()
|
|
|
|
to.subscribers[a] = &topicSubscriber{
|
|
|
|
userID: "a",
|
|
|
|
subscriber: nil,
|
|
|
|
cancel: func() {},
|
|
|
|
}
|
|
|
|
|
|
|
|
subFn := func(v *visitor, msg *message) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 02:04:49 +00:00
|
|
|
//lint:ignore SA1019 Force rand.Int to generate the same id once more
|
2023-05-02 16:10:27 +00:00
|
|
|
rand.Seed(1)
|
|
|
|
id := to.Subscribe(subFn, "b", func() {})
|
|
|
|
res := to.subscribers[id]
|
|
|
|
|
2023-05-02 18:16:59 +00:00
|
|
|
require.NotEqual(t, id, a)
|
|
|
|
require.Equal(t, "b", res.userID, "b")
|
2023-05-02 16:10:27 +00:00
|
|
|
}
|