2020-01-15 07:34:18 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
2020-07-19 01:33:07 +00:00
|
|
|
"context"
|
2020-01-15 07:34:18 +00:00
|
|
|
"log"
|
2020-04-06 19:03:42 +00:00
|
|
|
"strings"
|
2020-01-15 07:34:18 +00:00
|
|
|
"testing"
|
2020-01-18 07:13:50 +00:00
|
|
|
"time"
|
2020-04-24 22:30:15 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/internal/heart"
|
2020-12-01 01:49:18 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/internal/testenv"
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/utils/wsutil"
|
2020-01-15 07:34:18 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 19:03:42 +00:00
|
|
|
func init() {
|
2020-04-24 22:30:15 +00:00
|
|
|
wsutil.WSDebug = func(v ...interface{}) {
|
2020-04-06 19:03:42 +00:00
|
|
|
log.Println(append([]interface{}{"Debug:"}, v...)...)
|
|
|
|
}
|
2020-07-30 19:44:07 +00:00
|
|
|
heart.Debug = func(v ...interface{}) {
|
|
|
|
log.Println(append([]interface{}{"Heart:"}, v...)...)
|
|
|
|
}
|
2020-04-06 19:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 01:29:23 +00:00
|
|
|
func TestInvalidToken(t *testing.T) {
|
|
|
|
g, err := NewGateway("bad token")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to make a Gateway:", err)
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:48:29 +00:00
|
|
|
if err = g.Open(); err == nil {
|
2020-03-17 01:29:23 +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") {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
2020-03-17 01:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:34:18 +00:00
|
|
|
func TestIntegration(t *testing.T) {
|
2020-12-01 01:49:18 +00:00
|
|
|
config := testenv.Must(t)
|
2020-01-15 07:34:18 +00:00
|
|
|
|
2020-04-24 22:30:15 +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.
|
2020-12-01 01:49:18 +00:00
|
|
|
g, err := NewGateway("Bot " + config.BotToken)
|
2020-01-15 07:34:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to make a Gateway:", err)
|
|
|
|
}
|
2020-10-30 19:00:41 +00:00
|
|
|
g.AddIntents(IntentGuilds)
|
2020-04-07 02:36:06 +00:00
|
|
|
g.AfterClose = func(err error) {
|
2021-01-01 07:48:29 +00:00
|
|
|
t.Log("Closed.")
|
2020-04-07 02:36:06 +00:00
|
|
|
}
|
2020-01-15 07:34:18 +00:00
|
|
|
gateway = g
|
|
|
|
|
2020-01-18 07:20:05 +00:00
|
|
|
if err := g.Open(); err != nil {
|
|
|
|
t.Fatal("Failed to authenticate with Discord:", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-18 07:40:44 +00:00
|
|
|
t.Fatal("Event received is not of type Ready:", ev)
|
2020-01-15 07:34:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
if gateway.SessionID() == "" {
|
2020-01-15 07:34:18 +00:00
|
|
|
t.Fatal("Session ID is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Bot's username is", ready.User.Username)
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
// Send a faster heartbeat every second for testing.
|
|
|
|
g.PacerLoop.SetPace(time.Second)
|
|
|
|
|
2020-04-06 19:03:42 +00:00
|
|
|
// Sleep past the rate limiter before reconnecting:
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2020-04-24 22:30:15 +00:00
|
|
|
gotimeout(t, func() {
|
2020-07-19 01:33:07 +00:00
|
|
|
// Try and reconnect for 20 seconds maximum.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := gateway.ReconnectCtx(ctx); err != nil {
|
2020-04-24 22:30:15 +00:00
|
|
|
t.Fatal("Unexpected error while reconnecting:", err)
|
|
|
|
}
|
|
|
|
})
|
2020-01-18 07:40:44 +00:00
|
|
|
|
2020-04-12 18:06:43 +00:00
|
|
|
// Wait for the desired event:
|
|
|
|
gotimeout(t, func() {
|
|
|
|
for ev := range gateway.Events {
|
2020-02-02 22:12:54 +00:00
|
|
|
switch ev.(type) {
|
2020-04-06 19:03:42 +00:00
|
|
|
// Accept only a Resumed event.
|
|
|
|
case *ResumedEvent:
|
2020-04-12 18:06:43 +00:00
|
|
|
return // exit
|
2020-04-06 19:03:42 +00:00
|
|
|
case *ReadyEvent:
|
|
|
|
t.Fatal("Ready event received instead of Resumed.")
|
2020-02-02 22:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 18:06:43 +00:00
|
|
|
})
|
2020-01-16 03:28:21 +00:00
|
|
|
|
2020-01-18 07:20:05 +00:00
|
|
|
if err := g.Close(); err != nil {
|
|
|
|
t.Fatal("Failed to close Gateway:", err)
|
|
|
|
}
|
2020-01-15 07:34:18 +00:00
|
|
|
}
|
2020-01-18 07:13:50 +00:00
|
|
|
|
|
|
|
func wait(t *testing.T, evCh chan interface{}) interface{} {
|
|
|
|
select {
|
|
|
|
case ev := <-evCh:
|
|
|
|
return ev
|
2020-07-11 19:50:32 +00:00
|
|
|
case <-time.After(20 * time.Second):
|
2020-01-18 07:13:50 +00:00
|
|
|
t.Fatal("Timed out waiting for event")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 18:06:43 +00:00
|
|
|
|
|
|
|
func gotimeout(t *testing.T, fn func()) {
|
2020-07-11 19:50:32 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-04-12 18:06:43 +00:00
|
|
|
var done = make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
fn()
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2020-07-11 19:50:32 +00:00
|
|
|
case <-time.After(20 * time.Second):
|
2020-04-12 18:06:43 +00:00
|
|
|
t.Fatal("Timed out waiting for function.")
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|