1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 04:24:19 +00:00
arikawa/gateway/integration_test.go

157 lines
3.1 KiB
Go
Raw Normal View History

2020-01-15 07:34:18 +00:00
package gateway
import (
"context"
2020-01-15 07:34:18 +00:00
"log"
"strings"
2020-01-15 07:34:18 +00:00
"testing"
"time"
2021-06-02 02:53:19 +00:00
"github.com/diamondburned/arikawa/v3/internal/heart"
"github.com/diamondburned/arikawa/v3/internal/testenv"
"github.com/diamondburned/arikawa/v3/utils/wsutil"
2020-01-15 07:34:18 +00:00
)
func init() {
wsutil.WSDebug = func(v ...interface{}) {
log.Println(append([]interface{}{"Debug:"}, v...)...)
}
heart.Debug = func(v ...interface{}) {
log.Println(append([]interface{}{"Heart:"}, v...)...)
}
}
2021-04-03 03:03:25 +00:00
func TestURL(t *testing.T) {
u, err := URL()
if err != nil {
t.Fatal("failed to get gateway URL:", err)
}
if u == "" {
t.Fatal("gateway URL is empty")
}
if !strings.HasPrefix(u, "wss://") {
t.Fatal("gatewayURL is invalid:", u)
}
}
func TestInvalidToken(t *testing.T) {
g, err := NewGateway("bad token")
if err != nil {
2021-04-07 18:42:13 +00:00
t.Fatal("failed to make a Gateway:", err)
}
2021-01-01 07:48:29 +00:00
if err = g.Open(); err == nil {
2021-04-07 18:42:13 +00:00
t.Fatal("unexpected success while opening with a bad token.")
}
// 4004 Authentication Failed.
2021-01-01 07:48:29 +00:00
if !strings.Contains(err.Error(), "4004") {
2021-04-07 18:42:13 +00:00
t.Fatal("unexpected error:", err)
}
}
2020-01-15 07:34:18 +00:00
func TestIntegration(t *testing.T) {
config := testenv.Must(t)
2020-01-15 07:34:18 +00:00
wsutil.WSError = func(err error) {
2020-10-30 18:15:32 +00:00
t.Error(err)
2020-01-15 07:34:18 +00:00
}
var gateway *Gateway
// NewGateway should call Start for us.
g, err := NewGateway("Bot " + config.BotToken)
2020-01-15 07:34:18 +00:00
if err != nil {
2021-04-07 18:42:13 +00:00
t.Fatal("failed to make a Gateway:", err)
2020-01-15 07:34:18 +00:00
}
g.AddIntents(IntentGuilds)
g.AfterClose = func(err error) {
2021-04-07 18:42:13 +00:00
t.Log("closed.")
}
2020-01-15 07:34:18 +00:00
gateway = g
2020-01-18 07:20:05 +00:00
if err := g.Open(); err != nil {
2021-04-07 18:42:13 +00:00
t.Fatal("failed to authenticate with Discord:", err)
2020-01-18 07:20:05 +00:00
}
2020-01-18 07:40:44 +00:00
ev := wait(t, gateway.Events)
ready, ok := ev.(*ReadyEvent)
2020-01-15 07:34:18 +00:00
if !ok {
2021-04-07 18:42:13 +00:00
t.Fatal("event received is not of type Ready:", ev)
2020-01-15 07:34:18 +00:00
}
if gateway.SessionID() == "" {
2021-04-07 18:42:13 +00:00
t.Fatal("session ID is empty")
2020-01-15 07:34:18 +00:00
}
log.Println("Bot's username is", ready.User.Username)
// Send a faster heartbeat every second for testing.
g.PacerLoop.SetPace(time.Second)
// Sleep past the rate limiter before reconnecting:
time.Sleep(5 * time.Second)
gotimeout(t, func() {
// Try and reconnect for 20 seconds maximum.
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
g.ErrorLog = func(err error) {
2021-04-07 18:42:13 +00:00
t.Error("unexpected error while reconnecting:", err)
}
2021-04-07 18:42:13 +00:00
if err := gateway.ReconnectCtx(ctx); err != nil {
t.Error("failed to reconnect Gateway:", err)
}
})
2020-01-18 07:40:44 +00:00
g.ErrorLog = func(err error) { log.Println(err) }
// Wait for the desired event:
gotimeout(t, func() {
for ev := range gateway.Events {
2020-02-02 22:12:54 +00:00
switch ev.(type) {
// Accept only a Resumed event.
case *ResumedEvent:
return // exit
case *ReadyEvent:
t.Fatal("Ready event received instead of Resumed.")
2020-02-02 22:12:54 +00:00
}
}
})
2020-01-16 03:28:21 +00:00
2020-01-18 07:20:05 +00:00
if err := g.Close(); err != nil {
2021-04-07 18:42:13 +00:00
t.Fatal("failed to close Gateway:", err)
2020-01-18 07:20:05 +00:00
}
2020-01-15 07:34:18 +00:00
}
func wait(t *testing.T, evCh chan interface{}) interface{} {
select {
case ev := <-evCh:
return ev
case <-time.After(20 * time.Second):
2021-04-07 18:42:13 +00:00
t.Fatal("timed out waiting for event")
return nil
}
}
func gotimeout(t *testing.T, fn func()) {
t.Helper()
var done = make(chan struct{})
go func() {
fn()
done <- struct{}{}
}()
select {
case <-time.After(20 * time.Second):
2021-04-07 18:42:13 +00:00
t.Fatal("timed out waiting for function.")
case <-done:
return
}
}