forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_managed_conn_test.go
70 lines (62 loc) · 1.9 KB
/
websocket_managed_conn_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
package slack_test
import (
"testing"
slacktest "github.com/lusis/slack-test"
"github.com/nlopes/slack"
"github.com/stretchr/testify/assert"
)
const (
testMessage = "test message"
testToken = "TEST_TOKEN"
)
func TestRTMSingleConnect(t *testing.T) {
// Set up the test server.
testServer := slacktest.NewTestServer()
go testServer.Start()
// Setup and start the RTM.
slack.APIURL = testServer.GetAPIURL()
api := slack.New(testToken)
rtm := api.NewRTM(slack.RTMOptionUseStart(true))
go rtm.ManageConnection()
// Observe incoming messages.
done := make(chan struct{})
connectingReceived := false
connectedReceived := false
testMessageReceived := false
go func() {
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.ConnectingEvent:
if connectingReceived {
t.Error("Received multiple connecting events.")
t.Fail()
}
connectingReceived = true
case *slack.ConnectedEvent:
if connectedReceived {
t.Error("Received multiple connected events.")
t.Fail()
}
connectedReceived = true
case *slack.MessageEvent:
if ev.Text == testMessage {
testMessageReceived = true
rtm.Disconnect()
done <- struct{}{}
return
}
t.Logf("Discarding message with content %+v", ev)
default:
t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev)
}
}
}()
// Send a message and sleep for some time to make sure the message can be processed client-side.
testServer.SendDirectMessageToBot(testMessage)
<-done
testServer.Stop()
// Verify that all expected events have been received by the RTM client.
assert.True(t, connectingReceived, "Should have received a connecting event from the RTM instance.")
assert.True(t, connectedReceived, "Should have received a connected event from the RTM instance.")
assert.True(t, testMessageReceived, "Should have received a test message from the server.")
}