about summary refs log tree commit diff
path: root/pkg/discord/gateway/heartbeat_test.go
blob: 18862cfca950b7761dd2ec6a53221358463648d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gateway_test

import (
	"context"
	"jinx/pkg/discord/gateway"
	"jinx/pkg/discord/mocks"
	"testing"
	"time"

	"github.com/rs/zerolog"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

func TestHeartBeats(t *testing.T) {
	// This is probably a flaky test, if it fails, change the error margin.
	// Also, Discord sets it's own interval acceptance margins, perhaps we should
	// use that instead of a hardcoded error margin.
	errorMargin := 2 * time.Millisecond

	logger := zerolog.New(zerolog.NewTestWriter(t))
	g := mocks.Gateway{}

	beats := make(chan struct{}, 1)

	g.On("Heartbeat").Return(nil).Run(func(_ mock.Arguments) {
		beats <- struct{}{}
	})

	heartbeat := gateway.NewHeartbeat(&logger, &g)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	interval := 5 * time.Millisecond // Every 5 ms, way faster than Discord's default.

	start := time.Now()
	heartbeat.Start(ctx, interval)

	<-beats
	jitter := time.Since(start)
	require.Less(t, jitter, interval+errorMargin, "the first jitter heartbeat should happen before the interval")
	start = time.Now()

	<-beats
	secondBeat := time.Now()
	require.WithinDuration(t, start.Add(interval), secondBeat, errorMargin, "the second beat should happen roughly with the interval")
	start = secondBeat

	<-beats
	thirdBeat := time.Now()
	require.WithinDuration(t, start.Add(interval), thirdBeat, errorMargin, "the third beat should happen roughly with the interval")

	g.AssertNumberOfCalls(t, "Heartbeat", 3)
	g.AssertExpectations(t)
}

func TestHeartRespondsToForce(t *testing.T) {
	logger := zerolog.New(zerolog.NewTestWriter(t))
	g := mocks.Gateway{}

	beats := make(chan struct{}, 1)

	g.On("Heartbeat").Return(nil).Run(func(_ mock.Arguments) {
		beats <- struct{}{}
	})

	heartbeat := gateway.NewHeartbeat(&logger, &g)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	interval := 1 * time.Hour // Abnormally long, so we don't accidentally trigger the normal heartbeat.

	heartbeat.Start(ctx, interval)

	time.Sleep(1 * time.Millisecond) // Wait for routine to start.

	start := time.Now()
	heartbeat.ForceHeartbeat()
	<-beats
	require.WithinDuration(t, start, time.Now(), 1*time.Millisecond, "the forced heartbeat should happen within 1ms")

	g.AssertNumberOfCalls(t, "Heartbeat", 1)
	g.AssertExpectations(t)
}

func TestHeartDiesOnContextCancel(t *testing.T) {
	logger := zerolog.New(zerolog.NewTestWriter(t))
	g := mocks.Gateway{}

	g.On("Heartbeat").Return(nil).Maybe()

	heartbeat := gateway.NewHeartbeat(&logger, &g)

	ctx, cancel := context.WithCancel(context.Background())

	interval := 1 * time.Millisecond

	heartbeat.Start(ctx, interval)

	cancel()

	time.Sleep(5 * time.Millisecond)

	g.AssertNotCalled(t, "Heartbeat")
	g.AssertExpectations(t)
}