2022-04-06 13:11:19 +02:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dummyPublisher struct {
|
2022-08-05 10:19:33 +02:00
|
|
|
lock sync.Mutex
|
2022-04-06 13:11:19 +02:00
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyPublisher) SendPresence(userID string, presence types.Presence, statusMsg *string) error {
|
2022-08-05 10:19:33 +02:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
2022-04-06 13:11:19 +02:00
|
|
|
d.count++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyDB struct{}
|
|
|
|
|
|
|
|
func (d dummyDB) UpdatePresence(ctx context.Context, userID string, presence types.Presence, statusMsg *string, lastActiveTS gomatrixserverlib.Timestamp, fromSync bool) (types.StreamPosition, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-12-08 08:25:03 +01:00
|
|
|
func (d dummyDB) GetPresences(ctx context.Context, userID []string) ([]*types.PresenceInternal, error) {
|
|
|
|
return []*types.PresenceInternal{}, nil
|
2022-04-06 13:11:19 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:12:40 +02:00
|
|
|
func (d dummyDB) PresenceAfter(ctx context.Context, after types.StreamPosition, filter gomatrixserverlib.EventFilter) (map[string]*types.PresenceInternal, error) {
|
2022-04-06 13:11:19 +02:00
|
|
|
return map[string]*types.PresenceInternal{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyDB) MaxStreamPositionForPresence(ctx context.Context) (types.StreamPosition, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:53:08 +02:00
|
|
|
type dummyConsumer struct{}
|
|
|
|
|
2022-07-07 16:29:25 +02:00
|
|
|
func (d dummyConsumer) EmitPresence(ctx context.Context, userID string, presence types.Presence, statusMsg *string, ts gomatrixserverlib.Timestamp, fromSync bool) {
|
2022-05-17 16:53:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:11:19 +02:00
|
|
|
func TestRequestPool_updatePresence(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
presence string
|
|
|
|
userID string
|
|
|
|
sleep time.Duration
|
|
|
|
}
|
|
|
|
publisher := &dummyPublisher{}
|
2022-05-17 16:53:08 +02:00
|
|
|
consumer := &dummyConsumer{}
|
2022-04-06 13:11:19 +02:00
|
|
|
syncMap := sync.Map{}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantIncrease bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "new presence is published",
|
|
|
|
wantIncrease: true,
|
|
|
|
args: args{
|
|
|
|
userID: "dummy",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "presence not published, no change",
|
|
|
|
args: args{
|
|
|
|
userID: "dummy",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "new presence is published dummy2",
|
|
|
|
wantIncrease: true,
|
|
|
|
args: args{
|
|
|
|
userID: "dummy2",
|
|
|
|
presence: "online",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different presence is published dummy2",
|
|
|
|
wantIncrease: true,
|
|
|
|
args: args{
|
|
|
|
userID: "dummy2",
|
|
|
|
presence: "unavailable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same presence is not published dummy2",
|
|
|
|
args: args{
|
|
|
|
userID: "dummy2",
|
|
|
|
presence: "unavailable",
|
|
|
|
sleep: time.Millisecond * 150,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same presence is published after being deleted",
|
|
|
|
wantIncrease: true,
|
|
|
|
args: args{
|
|
|
|
userID: "dummy2",
|
|
|
|
presence: "unavailable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rp := &RequestPool{
|
|
|
|
presence: &syncMap,
|
|
|
|
producer: publisher,
|
2022-05-17 16:53:08 +02:00
|
|
|
consumer: consumer,
|
2022-04-06 13:11:19 +02:00
|
|
|
cfg: &config.SyncAPI{
|
|
|
|
Matrix: &config.Global{
|
|
|
|
JetStream: config.JetStream{
|
|
|
|
TopicPrefix: "Dendrite",
|
|
|
|
},
|
|
|
|
Presence: config.PresenceOptions{
|
|
|
|
EnableInbound: true,
|
|
|
|
EnableOutbound: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := dummyDB{}
|
|
|
|
go rp.cleanPresence(db, time.Millisecond*50)
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-08-05 10:19:33 +02:00
|
|
|
publisher.lock.Lock()
|
2022-04-06 13:11:19 +02:00
|
|
|
beforeCount := publisher.count
|
2022-08-05 10:19:33 +02:00
|
|
|
publisher.lock.Unlock()
|
2022-04-06 13:11:19 +02:00
|
|
|
rp.updatePresence(db, tt.args.presence, tt.args.userID)
|
2022-08-05 10:19:33 +02:00
|
|
|
publisher.lock.Lock()
|
2022-04-06 13:11:19 +02:00
|
|
|
if tt.wantIncrease && publisher.count <= beforeCount {
|
|
|
|
t.Fatalf("expected count to increase: %d <= %d", publisher.count, beforeCount)
|
|
|
|
}
|
2022-08-05 10:19:33 +02:00
|
|
|
publisher.lock.Unlock()
|
2022-04-06 13:11:19 +02:00
|
|
|
time.Sleep(tt.args.sleep)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|