-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublicchannels_test.go
117 lines (108 loc) · 2.69 KB
/
publicchannels_test.go
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
package main
import (
"fmt"
"strconv"
"testing"
"github.com/gotify/plugin-api"
. "github.com/smartystreets/goconvey/convey"
)
func shouldAllBePublicChannel(actual interface{}, expected ...interface{}) string {
var list []ChannelDef
switch actual := actual.(type) {
case []ChannelDef:
list = actual
case []ChannelWithUserContext:
for _, def := range actual {
list = append(list, def.Channel)
}
case ChannelDef:
list = append(list, actual)
case ChannelWithUserContext:
list = append(list, actual.Channel)
}
for _, def := range list {
if !def.Public {
return fmt.Sprintf("channel %s is not public", def.Name)
}
}
return ""
}
func TestPublicChannels(t *testing.T) {
Convey("Test Public Channel Registry", t, func(c C) {
registry := new(PublicChannelListManager)
c.So(registry.GetAllChannels(), ShouldBeEmpty)
c.Convey("adds single user", func(c C) {
registry.UpdateChannelsForUser(plugin.UserContext{
ID: 1,
Name: "test",
Admin: true,
}, []ChannelDef{
{"test_channel", true},
{"test_private_channel", false},
})
c.So(registry.GetAllChannels(), ShouldHaveLength, 1)
c.So(registry.GetAllChannels(), shouldAllBePublicChannel)
})
c.Convey("updates user", func(c C) {
registry.UpdateChannelsForUser(plugin.UserContext{
ID: 1,
Name: "test",
Admin: true,
}, []ChannelDef{
{"test_private_channel", true},
{"test_public_channel", true},
})
c.So(registry.GetAllChannels(), ShouldHaveLength, 2)
c.So(registry.GetAllChannels(), shouldAllBePublicChannel)
})
c.Convey("sync safety", func(c C) {
registry.UpdateChannelsForUser(plugin.UserContext{
ID: 1,
Name: "test_1",
Admin: true,
}, []ChannelDef{
{"test_channel", true},
{"test_private_channel", false},
})
generaterChan := make(chan struct{})
go func() {
for i := 2; i < 1000; i++ {
registry.UpdateChannelsForUser(plugin.UserContext{
ID: 1,
Name: "test_1",
Admin: true,
}, []ChannelDef{
{"test_channel", true},
{"test_private_channel", false},
})
registry.UpdateChannelsForUser(plugin.UserContext{
ID: uint(i),
Name: "test_" + strconv.Itoa(i),
Admin: true,
}, []ChannelDef{
{"test_channel", true},
{"test_private_channel", false},
})
}
close(generaterChan)
}()
done := false
for !done {
channels := registry.GetAllChannels()
c.So(channels, shouldAllBePublicChannel)
uID1ChanCount := 0
for _, c := range channels {
if c.UserContext.ID == 1 {
uID1ChanCount++
}
}
c.So(uID1ChanCount, ShouldEqual, 1)
select {
case <-generaterChan:
done = true
default:
}
}
})
})
}