mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-01 04:24:19 +00:00
diamondburned
f1f052180b
This commit consists of these smaller commits: Gateway: SessionID to be a method for thread safety This commit breaks the SessionID field of the Gateway struct to be thread-safe by wrapping its access with a read-write mutex. As this is a bug fix, it is reasonable of a breaking change Heart: Allow later binding of event channel Voice: Use the new Heart API Heart: Fixed data races Heart: Allow changing pace, thread-safe Heartbeat
141 lines
2.8 KiB
Go
141 lines
2.8 KiB
Go
// +build !unitonly
|
|
|
|
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/diamondburned/arikawa/v2/internal/heart"
|
|
"github.com/diamondburned/arikawa/v2/internal/testenv"
|
|
"github.com/diamondburned/arikawa/v2/utils/wsutil"
|
|
)
|
|
|
|
func init() {
|
|
wsutil.WSDebug = func(v ...interface{}) {
|
|
log.Println(append([]interface{}{"Debug:"}, v...)...)
|
|
}
|
|
heart.Debug = func(v ...interface{}) {
|
|
log.Println(append([]interface{}{"Heart:"}, v...)...)
|
|
}
|
|
}
|
|
|
|
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.
|
|
if strings.Contains(err.Error(), "4004") {
|
|
return
|
|
}
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
}
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
config := testenv.Must(t)
|
|
|
|
wsutil.WSError = func(err error) {
|
|
t.Error(err)
|
|
}
|
|
|
|
var gateway *Gateway
|
|
|
|
// NewGateway should call Start for us.
|
|
g, err := NewGateway("Bot " + config.BotToken)
|
|
if err != nil {
|
|
t.Fatal("Failed to make a Gateway:", err)
|
|
}
|
|
g.AddIntents(IntentGuilds)
|
|
g.AfterClose = func(err error) {
|
|
log.Println("Closed.")
|
|
}
|
|
gateway = g
|
|
|
|
if err := g.Open(); err != nil {
|
|
t.Fatal("Failed to authenticate with Discord:", err)
|
|
}
|
|
|
|
ev := wait(t, gateway.Events)
|
|
ready, ok := ev.(*ReadyEvent)
|
|
if !ok {
|
|
t.Fatal("Event received is not of type Ready:", ev)
|
|
}
|
|
|
|
if gateway.SessionID() == "" {
|
|
t.Fatal("Session ID is empty")
|
|
}
|
|
|
|
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()
|
|
|
|
if err := gateway.ReconnectCtx(ctx); err != nil {
|
|
t.Fatal("Unexpected error while reconnecting:", err)
|
|
}
|
|
})
|
|
|
|
// Wait for the desired event:
|
|
gotimeout(t, func() {
|
|
for ev := range gateway.Events {
|
|
switch ev.(type) {
|
|
// Accept only a Resumed event.
|
|
case *ResumedEvent:
|
|
return // exit
|
|
case *ReadyEvent:
|
|
t.Fatal("Ready event received instead of Resumed.")
|
|
}
|
|
}
|
|
})
|
|
|
|
if err := g.Close(); err != nil {
|
|
t.Fatal("Failed to close Gateway:", err)
|
|
}
|
|
}
|
|
|
|
func wait(t *testing.T, evCh chan interface{}) interface{} {
|
|
select {
|
|
case ev := <-evCh:
|
|
return ev
|
|
case <-time.After(20 * time.Second):
|
|
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):
|
|
t.Fatal("Timed out waiting for function.")
|
|
case <-done:
|
|
return
|
|
}
|
|
}
|