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
// possible. If an Invalid Session is received, it will start a fresh one.
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
// 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...")
// 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
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)
return
return nil
}
}

View File

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