2020-01-15 07:34:18 +00:00
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
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-01-15 07:34:18 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 19:03:42 +00:00
|
|
|
func init() {
|
|
|
|
WSDebug = func(v ...interface{}) {
|
|
|
|
log.Println(append([]interface{}{"Debug:"}, v...)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.Open()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected success while opening with a bad token.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4004 Authentication Failed.
|
2020-04-06 19:03:42 +00:00
|
|
|
if strings.Contains(err.Error(), "4004") {
|
2020-03-17 01:29:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:34:18 +00:00
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
var token = os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
t.Fatal("Missing $BOT_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
WSError = func(err error) {
|
2020-02-02 22:12:54 +00:00
|
|
|
t.Fatal(err)
|
2020-01-15 07:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var gateway *Gateway
|
|
|
|
|
|
|
|
// NewGateway should call Start for us.
|
2020-01-16 04:27:57 +00:00
|
|
|
g, err := NewGateway("Bot " + token)
|
2020-01-15 07:34:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to make a Gateway:", err)
|
|
|
|
}
|
2020-04-07 02:36:06 +00:00
|
|
|
g.AfterClose = func(err error) {
|
|
|
|
log.Println("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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if gateway.SessionID == "" {
|
|
|
|
t.Fatal("Session ID is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Bot's username is", ready.User.Username)
|
|
|
|
|
2020-04-06 19:03:42 +00:00
|
|
|
// Sleep past the rate limiter before reconnecting:
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2020-01-15 07:34:18 +00:00
|
|
|
// Try and reconnect
|
|
|
|
if err := gateway.Reconnect(); err != nil {
|
|
|
|
t.Fatal("Failed to reconnect:", err)
|
|
|
|
}
|
|
|
|
|
2020-02-02 22:12:54 +00:00
|
|
|
timeout := time.After(10 * time.Second)
|
2020-01-18 07:40:44 +00:00
|
|
|
|
2020-02-02 22:12:54 +00:00
|
|
|
Main:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-gateway.Events:
|
|
|
|
switch ev.(type) {
|
2020-04-06 19:03:42 +00:00
|
|
|
// Accept only a Resumed event.
|
|
|
|
case *ResumedEvent:
|
2020-02-02 22:12:54 +00:00
|
|
|
break Main
|
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
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("Timed out waiting for ResumedEvent")
|
|
|
|
}
|
2020-01-15 07:34:18 +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
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("Timed out waiting for event")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|