-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathslack_test.go
79 lines (71 loc) · 1.99 KB
/
slack_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
package slacknimate
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slacktest"
)
func TestUpdater(t *testing.T) {
// slacktest module is pretty bare bones, and doesn't support chat.update
// API post which is the core of our functionality. So patch in a very
// rudimentary handler to just register that we got the updates.
testServer := slacktest.NewTestServer()
var serverChatUpdate int
testServer.Handle("/chat.update", func(w http.ResponseWriter, r *http.Request) {
serverChatUpdate++
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"ok": true}`))
})
// start up the test server and configure an API client
testServer.Start()
defer testServer.Stop()
client := slack.New("ABCD123",
slack.OptionAPIURL(testServer.GetAPIURL()),
slack.OptionDebug(false),
)
// goroutine to generate a few test frames and then close
const numTestFrames = 10
frames := testFrameGenerator(context.Background(), numTestFrames)
// ctx, cf := context.WithCancel(context.Background())
ctx := context.Background()
var callbacksSeen int
err := Updater(ctx, client, "#testing", frames, UpdaterOptions{
UpdateFunc: func(u Update) {
callbacksSeen++
if err := u.Err; err != nil {
t.Errorf("%#v", err)
}
},
})
if err != nil {
t.Fatal("Updater fatal err:", err)
}
if callbacksSeen != numTestFrames {
t.Errorf(
"client callbacks seen want %v got %v",
numTestFrames, callbacksSeen)
}
if serverChatUpdate != numTestFrames-1 {
t.Errorf(
"server chat.update posts received want %v got %v",
numTestFrames-1, serverChatUpdate)
}
}
// testFrameGenerator creates a background goroutine which will send n mock
// frame updates over the returned channel
func testFrameGenerator(ctx context.Context, n uint) <-chan string {
frames := make(chan string)
go func() {
defer close(frames)
for i := uint(0); i < n; i++ {
select {
case frames <- fmt.Sprintf("frame%v", i):
case <-ctx.Done():
return
}
}
}()
return frames
}