aboutsummaryrefslogtreecommitdiff
path: root/syncapi/sync/requestpool_test.go
blob: e083507e88ec564d19bf0f50916eca4cbf45bf46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sync

import (
	"context"
	"sync"
	"testing"
	"time"

	"github.com/matrix-org/dendrite/setup/config"
	"github.com/matrix-org/dendrite/syncapi/synctypes"
	"github.com/matrix-org/dendrite/syncapi/types"
	"github.com/matrix-org/gomatrixserverlib/spec"
)

type dummyPublisher struct {
	lock  sync.Mutex
	count int
}

func (d *dummyPublisher) SendPresence(userID string, presence types.Presence, statusMsg *string) error {
	d.lock.Lock()
	defer d.lock.Unlock()
	d.count++
	return nil
}

type dummyDB struct{}

func (d dummyDB) UpdatePresence(ctx context.Context, userID string, presence types.Presence, statusMsg *string, lastActiveTS spec.Timestamp, fromSync bool) (types.StreamPosition, error) {
	return 0, nil
}

func (d dummyDB) GetPresences(ctx context.Context, userID []string) ([]*types.PresenceInternal, error) {
	return []*types.PresenceInternal{}, nil
}

func (d dummyDB) PresenceAfter(ctx context.Context, after types.StreamPosition, filter synctypes.EventFilter) (map[string]*types.PresenceInternal, error) {
	return map[string]*types.PresenceInternal{}, nil
}

func (d dummyDB) MaxStreamPositionForPresence(ctx context.Context) (types.StreamPosition, error) {
	return 0, nil
}

type dummyConsumer struct{}

func (d dummyConsumer) EmitPresence(ctx context.Context, userID string, presence types.Presence, statusMsg *string, ts spec.Timestamp, fromSync bool) {

}

func TestRequestPool_updatePresence(t *testing.T) {
	type args struct {
		presence string
		userID   string
		sleep    time.Duration
	}
	publisher := &dummyPublisher{}
	consumer := &dummyConsumer{}
	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",
			},
		},
		/*
			TODO: Fixme
					{
						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,
		consumer: consumer,
		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) {
			publisher.lock.Lock()
			beforeCount := publisher.count
			publisher.lock.Unlock()
			rp.updatePresence(db, tt.args.presence, tt.args.userID)
			publisher.lock.Lock()
			if tt.wantIncrease && publisher.count <= beforeCount {
				t.Fatalf("expected count to increase: %d <= %d", publisher.count, beforeCount)
			}
			publisher.lock.Unlock()
			time.Sleep(tt.args.sleep)
		})
	}
}