1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-10 17:05:17 +00:00

Gateway: ReconnectCtx now returns error; fixed test

This commit is contained in:
diamondburned 2020-07-18 18:33:07 -07:00
parent a929817c0f
commit 24f7ed0499
2 changed files with 16 additions and 6 deletions

View file

@ -203,12 +203,18 @@ func (g *Gateway) Close() error {
// Reconnect tries to reconnect forever. It will resume the connection if // Reconnect tries to reconnect forever. It will resume the connection if
// possible. If an Invalid Session is received, it will start a fresh one. // possible. If an Invalid Session is received, it will start a fresh one.
func (g *Gateway) Reconnect() { func (g *Gateway) Reconnect() {
g.ReconnectCtx(context.Background()) for {
if err := g.ReconnectCtx(context.Background()); err != nil {
g.ErrorLog(err)
} else {
return
}
}
} }
// ReconnectCtx attempts to reconnect until context expires. If context cannot // ReconnectCtx attempts to reconnect until context expires. If context cannot
// expire, then the gateway will try to reconnect forever. // expire, then the gateway will try to reconnect forever.
func (g *Gateway) ReconnectCtx(ctx context.Context) { func (g *Gateway) ReconnectCtx(ctx context.Context) error {
wsutil.WSDebug("Reconnecting...") wsutil.WSDebug("Reconnecting...")
// Guarantee the gateway is already closed. Ignore its error, as we're // Guarantee the gateway is already closed. Ignore its error, as we're
@ -223,11 +229,11 @@ func (g *Gateway) ReconnectCtx(ctx context.Context) {
// https://discordapp.com/developers/docs/topics/gateway#rate-limiting // https://discordapp.com/developers/docs/topics/gateway#rate-limiting
if err := g.OpenContext(ctx); err != nil { if err := g.OpenContext(ctx); err != nil {
g.ErrorLog(errors.Wrap(err, "failed to open gateway")) return errors.Wrap(err, "failed to open gateway")
} }
wsutil.WSDebug("Started after attempt:", i) wsutil.WSDebug("Started after attempt:", i)
return return nil
} }
} }

View file

@ -3,6 +3,7 @@
package gateway package gateway
import ( import (
"context"
"log" "log"
"os" "os"
"strings" "strings"
@ -78,9 +79,12 @@ func TestIntegration(t *testing.T) {
// Sleep past the rate limiter before reconnecting: // Sleep past the rate limiter before reconnecting:
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
// Try and reconnect forever:
gotimeout(t, func() { gotimeout(t, func() {
if err := gateway.Reconnect(); err != nil { // 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) t.Fatal("Unexpected error while reconnecting:", err)
} }
}) })