| 1 |
package slack_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
websocket "github.com/gorilla/websocket" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"github.com/slack-go/slack" |
| 16 |
"github.com/slack-go/slack/slacktest" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
testMessage = "test message" |
| 21 |
testToken = "TEST_TOKEN" |
| 22 |
) |
| 23 |
|
| 24 |
func TestRTMBeforeEvents(t *testing.T) { |
| 25 |
// Set up the test server. |
| 26 |
testServer := slacktest.NewTestServer() |
| 27 |
go testServer.Start() |
| 28 |
|
| 29 |
// Setup and start the RTM. |
| 30 |
api := slack.New(testToken, slack.OptionAPIURL(testServer.GetAPIURL())) |
| 31 |
rtm := api.NewRTM() |
| 32 |
|
| 33 |
done := make(chan struct{}) |
| 34 |
go func() { |
| 35 |
for msg := range rtm.IncomingEvents { |
| 36 |
switch ev := msg.Data.(type) { |
| 37 |
case *slack.DisconnectedEvent: |
| 38 |
if ev.Intentional { |
| 39 |
close(done) |
| 40 |
return |
| 41 |
} |
| 42 |
default: |
| 43 |
// t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 44 |
} |
| 45 |
} |
| 46 |
}() |
| 47 |
go rtm.Disconnect() |
| 48 |
go rtm.ManageConnection() |
| 49 |
select { |
| 50 |
case <-done: |
| 51 |
case <-time.After(5 * time.Second): |
| 52 |
t.Error("timed out waiting for disconnect") |
| 53 |
t.Fail() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestRTMGoodbye(t *testing.T) { |
| 58 |
// Set up the test server. |
| 59 |
testServer := slacktest.NewTestServer( |
| 60 |
func(c slacktest.Customize) { |
| 61 |
c.Handle("/ws", slacktest.Websocket(func(conn *websocket.Conn) { |
| 62 |
if err := slacktest.RTMServerSendGoodbye(conn); err != nil { |
| 63 |
log.Println("failed to send goodbye", err) |
| 64 |
} |
| 65 |
})) |
| 66 |
}, |
| 67 |
) |
| 68 |
go testServer.Start() |
| 69 |
|
| 70 |
// Setup and start the RTM. |
| 71 |
api := slack.New( |
| 72 |
testToken, |
| 73 |
slack.OptionAPIURL(testServer.GetAPIURL()), |
| 74 |
) |
| 75 |
|
| 76 |
rtm := api.NewRTM( |
| 77 |
slack.RTMOptionPingInterval(100 * time.Millisecond), |
| 78 |
) |
| 79 |
|
| 80 |
done := make(chan struct{}) |
| 81 |
go rtm.ManageConnection() |
| 82 |
connected := 0 |
| 83 |
disconnected := 0 |
| 84 |
func() { |
| 85 |
for msg := range rtm.IncomingEvents { |
| 86 |
switch ev := msg.Data.(type) { |
| 87 |
case *slack.ConnectedEvent: |
| 88 |
connected += 1 |
| 89 |
if connected > 5 { |
| 90 |
rtm.Disconnect() |
| 91 |
} |
| 92 |
case *slack.DisconnectedEvent: |
| 93 |
// t.Log("disconnect event received", ev.Intentional, ev.Cause) |
| 94 |
if ev.Intentional { |
| 95 |
close(done) |
| 96 |
return |
| 97 |
} |
| 98 |
disconnected += 1 |
| 99 |
default: |
| 100 |
// t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 101 |
} |
| 102 |
} |
| 103 |
}() |
| 104 |
|
| 105 |
select { |
| 106 |
case <-done: |
| 107 |
// magic numbers from empirical testing. |
| 108 |
assert.Equal(t, connected <= 7, true) |
| 109 |
assert.Equal(t, disconnected <= 12, true) |
| 110 |
case <-time.After(5 * time.Second): |
| 111 |
t.Error("timed out waiting for disconnect") |
| 112 |
t.Fail() |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestRTMDeadConnection(t *testing.T) { |
| 117 |
// Set up the test server. |
| 118 |
testServer := slacktest.NewTestServer( |
| 119 |
func(c slacktest.Customize) { |
| 120 |
c.Handle("/ws", slacktest.Websocket(func(conn *websocket.Conn) { |
| 121 |
// closes immediately |
| 122 |
})) |
| 123 |
}, |
| 124 |
) |
| 125 |
go testServer.Start() |
| 126 |
|
| 127 |
// Setup and start the RTM. |
| 128 |
api := slack.New( |
| 129 |
testToken, |
| 130 |
slack.OptionAPIURL(testServer.GetAPIURL()), |
| 131 |
) |
| 132 |
|
| 133 |
rtm := api.NewRTM( |
| 134 |
slack.RTMOptionPingInterval(100 * time.Millisecond), |
| 135 |
) |
| 136 |
|
| 137 |
go rtm.ManageConnection() |
| 138 |
done := make(chan struct{}) |
| 139 |
connected := 0 |
| 140 |
disconnected := 0 |
| 141 |
func() { |
| 142 |
for msg := range rtm.IncomingEvents { |
| 143 |
switch ev := msg.Data.(type) { |
| 144 |
case *slack.ConnectedEvent: |
| 145 |
connected += 1 |
| 146 |
if connected > 5 { |
| 147 |
rtm.Disconnect() |
| 148 |
} |
| 149 |
case *slack.DisconnectedEvent: |
| 150 |
// t.Log("disconnect event received", ev.Intentional, ev.Cause) |
| 151 |
if ev.Intentional { |
| 152 |
close(done) |
| 153 |
return |
| 154 |
} |
| 155 |
disconnected += 1 |
| 156 |
default: |
| 157 |
// t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 158 |
} |
| 159 |
} |
| 160 |
}() |
| 161 |
|
| 162 |
select { |
| 163 |
case <-done: |
| 164 |
// magic numbers from empirical testing. |
| 165 |
assert.Equal(t, connected <= 7, true) |
| 166 |
assert.Equal(t, disconnected <= 7, true) |
| 167 |
case <-time.After(5 * time.Second): |
| 168 |
t.Error("timed out waiting for disconnect") |
| 169 |
t.Fail() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestRTMDisconnect(t *testing.T) { |
| •174 |
t.Skip() |
| 175 |
// actually connect to slack here w/ an invalid token |
| 176 |
api := slack.New(testToken) |
| 177 |
rtm := api.NewRTM() |
| 178 |
go rtm.ManageConnection() |
| 179 |
|
| 180 |
// Observe incoming messages. |
| 181 |
done := make(chan struct{}) |
| 182 |
connectingReceived := false |
| 183 |
disconnectedReceived := false |
| 184 |
|
| 185 |
go func() { |
| 186 |
for msg := range rtm.IncomingEvents { |
| 187 |
switch ev := msg.Data.(type) { |
| 188 |
case *slack.InvalidAuthEvent: |
| 189 |
t.Log("invalid auth event received") |
| 190 |
disconnectedReceived = true |
| 191 |
close(done) |
| 192 |
case *slack.ConnectingEvent: |
| 193 |
connectingReceived = true |
| 194 |
case *slack.ConnectedEvent: |
| 195 |
t.Error("received connected events on an invalid connection") |
| 196 |
t.Fail() |
| 197 |
default: |
| 198 |
t.Logf("discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 199 |
} |
| 200 |
} |
| 201 |
}() |
| 202 |
|
| 203 |
select { |
| 204 |
case <-done: |
| 205 |
case <-time.After(5 * time.Second): |
| 206 |
t.Error("timed out waiting for disconnect") |
| 207 |
t.Fail() |
| 208 |
} |
| 209 |
|
| 210 |
// Verify that all expected events have been received by the RTM client. |
| 211 |
assert.True(t, connectingReceived, "Should have received a connecting event from the RTM instance.") |
| 212 |
assert.True(t, disconnectedReceived, "Should have received a disconnected event from the RTM instance.") |
| 213 |
} |
| 214 |
|
| 215 |
func TestRTMConnectRateLimit(t *testing.T) { |
| 216 |
// Set up the test server. |
| 217 |
testServer := slacktest.NewTestServer( |
| 218 |
func(c slacktest.Customize) { |
| 219 |
c.Handle("/rtm.connect", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 220 |
w.Header().Add("Retry-After", "1") |
| 221 |
w.WriteHeader(http.StatusTooManyRequests) |
| 222 |
})) |
| 223 |
}, |
| 224 |
) |
| 225 |
go testServer.Start() |
| 226 |
|
| 227 |
// Setup and start the RTM. |
| 228 |
api := slack.New(testToken, slack.OptionAPIURL(testServer.GetAPIURL())) |
| 229 |
rtm := api.NewRTM() |
| 230 |
go rtm.ManageConnection() |
| 231 |
|
| 232 |
// Observe incoming failures |
| 233 |
connectionFailure := make(chan *slack.ConnectionErrorEvent) |
| 234 |
go func() { |
| 235 |
for msg := range rtm.IncomingEvents { |
| 236 |
switch ev := msg.Data.(type) { |
| 237 |
case *slack.ConnectingEvent: |
| 238 |
case *slack.ConnectionErrorEvent: |
| 239 |
connectionFailure <- ev |
| 240 |
if ev.Attempt > 5 { |
| 241 |
rtm.Disconnect() |
| 242 |
} |
| 243 |
case *slack.DisconnectedEvent: |
| 244 |
if ev.Intentional { |
| 245 |
close(connectionFailure) |
| 246 |
return |
| 247 |
} |
| 248 |
default: |
| 249 |
t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 250 |
} |
| 251 |
} |
| 252 |
}() |
| 253 |
|
| 254 |
previous := time.Duration(0) |
| 255 |
for ev := range connectionFailure { |
| 256 |
assert.True(t, previous <= ev.Backoff, fmt.Sprintf("backoff should increase during rate limits: %v <= %v", previous, ev.Backoff)) |
| 257 |
previous = ev.Backoff |
| 258 |
} |
| 259 |
testServer.Stop() |
| 260 |
} |
| 261 |
|
| 262 |
func TestRTMSingleConnect(t *testing.T) { |
| 263 |
// Set up the test server. |
| 264 |
testServer := slacktest.NewTestServer() |
| 265 |
go testServer.Start() |
| 266 |
|
| 267 |
// Setup and start the RTM. |
| 268 |
api := slack.New(testToken, slack.OptionAPIURL(testServer.GetAPIURL())) |
| 269 |
rtm := api.NewRTM() |
| 270 |
go rtm.ManageConnection() |
| 271 |
|
| 272 |
// Observe incoming messages. |
| 273 |
done := make(chan struct{}) |
| 274 |
connectingReceived := false |
| 275 |
connectedReceived := false |
| 276 |
testMessageReceived := false |
| 277 |
go func() { |
| 278 |
for msg := range rtm.IncomingEvents { |
| 279 |
switch ev := msg.Data.(type) { |
| 280 |
case *slack.ConnectingEvent: |
| 281 |
if connectingReceived { |
| 282 |
t.Error("Received multiple connecting events.") |
| 283 |
t.Fail() |
| 284 |
} |
| 285 |
connectingReceived = true |
| 286 |
case *slack.ConnectedEvent: |
| 287 |
if connectedReceived { |
| 288 |
t.Error("Received multiple connected events.") |
| 289 |
t.Fail() |
| 290 |
} |
| 291 |
connectedReceived = true |
| 292 |
case *slack.MessageEvent: |
| 293 |
if ev.Text == testMessage { |
| 294 |
testMessageReceived = true |
| 295 |
rtm.Disconnect() |
| 296 |
} |
| 297 |
t.Logf("Discarding message with content %+v", ev) |
| 298 |
case *slack.DisconnectedEvent: |
| 299 |
if ev.Intentional { |
| 300 |
done <- struct{}{} |
| 301 |
return |
| 302 |
} |
| 303 |
default: |
| 304 |
t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 305 |
} |
| 306 |
} |
| 307 |
}() |
| 308 |
|
| 309 |
// Send a message and sleep for some time to make sure the message can be processed client-side. |
| 310 |
testServer.SendDirectMessageToBot(testMessage) |
| 311 |
<-done |
| 312 |
testServer.Stop() |
| 313 |
|
| 314 |
// Verify that all expected events have been received by the RTM client. |
| 315 |
assert.True(t, connectingReceived, "Should have received a connecting event from the RTM instance.") |
| 316 |
assert.True(t, connectedReceived, "Should have received a connected event from the RTM instance.") |
| 317 |
assert.True(t, testMessageReceived, "Should have received a test message from the server.") |
| 318 |
} |
| 319 |
|
| 320 |
func TestRTMUnmappedError(t *testing.T) { |
| 321 |
const unmappedEventName = "user_status_changed" |
| 322 |
// Set up the test server. |
| 323 |
testServer := slacktest.NewTestServer() |
| 324 |
go testServer.Start() |
| 325 |
|
| 326 |
// Setup and start the RTM. |
| 327 |
api := slack.New(testToken, slack.OptionAPIURL(testServer.GetAPIURL())) |
| 328 |
rtm := api.NewRTM() |
| 329 |
go rtm.ManageConnection() |
| 330 |
|
| 331 |
// Observe incoming messages. |
| 332 |
done := make(chan struct{}) |
| 333 |
var gotUnmarshallingError *slack.UnmarshallingErrorEvent |
| 334 |
go func() { |
| 335 |
for msg := range rtm.IncomingEvents { |
| 336 |
switch ev := msg.Data.(type) { |
| 337 |
case *slack.UnmarshallingErrorEvent: |
| 338 |
gotUnmarshallingError = ev |
| 339 |
rtm.Disconnect() |
| 340 |
case *slack.DisconnectedEvent: |
| 341 |
if ev.Intentional { |
| 342 |
done <- struct{}{} |
| 343 |
return |
| 344 |
} |
| 345 |
default: |
| 346 |
t.Logf("Discarded event of type '%s' with content '%#v'", msg.Type, ev) |
| 347 |
} |
| 348 |
} |
| 349 |
}() |
| 350 |
|
| 351 |
// Send a message and sleep for some time to make sure the message can be processed client-side. |
| 352 |
testServer.SendToWebsocket(fixSlackMessage(t, unmappedEventName)) |
| 353 |
<-done |
| 354 |
testServer.Stop() |
| 355 |
|
| 356 |
// Verify that we got the expected error with details |
| 357 |
unmappedErr, ok := gotUnmarshallingError.ErrorObj.(*slack.UnmappedError) |
| 358 |
require.True(t, ok) |
| 359 |
assert.Equal(t, unmappedEventName, unmappedErr.EventType) |
| 360 |
} |
| 361 |
|
| 362 |
func fixSlackMessage(t *testing.T, eType string) string { |
| 363 |
t.Helper() |
| 364 |
|
| 365 |
m := slack.Message{ |
| 366 |
Msg: slack.Msg{ |
| 367 |
Type: eType, |
| 368 |
Text: "Fixture Slack message", |
| 369 |
Timestamp: fmt.Sprintf("%d", time.Now().Unix()), |
| 370 |
}, |
| 371 |
} |
| 372 |
msg, err := json.Marshal(m) |
| 373 |
require.NoError(t, err) |
| 374 |
|
| 375 |
return string(msg) |
| 376 |
} |