-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend_test.go
162 lines (134 loc) · 5.09 KB
/
backend_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package minimatch
import (
"context"
"log"
"testing"
"time"
"github.com/bojand/hri"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
pb "github.com/castaneai/minimatch/gen/openmatch"
"github.com/castaneai/minimatch/pkg/statestore"
)
func TestValidateTicketExistenceBeforeAssign(t *testing.T) {
frontStore, backStore, _ := NewStateStoreWithMiniRedis(t)
ctx := context.Background()
t.Run("ValidationEnabled", func(t *testing.T) {
backend, err := NewBackend(backStore, AssignerFunc(dummyAssign), WithTicketValidationBeforeAssign(true))
require.NoError(t, err)
backend.AddMatchFunction(anyProfile, MatchFunctionSimple1vs1)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t1"}, defaultTicketTTL)
require.NoError(t, err)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t2"}, defaultTicketTTL)
require.NoError(t, err)
activeTickets, err := backend.fetchActiveTickets(ctx, defaultFetchTicketsLimit)
require.NoError(t, err)
require.Len(t, activeTickets, 2)
matches, err := backend.makeMatches(ctx, activeTickets)
require.NoError(t, err)
require.Len(t, matches, 1)
// Delete "t1" after match is established
err = frontStore.DeleteTicket(ctx, "t1")
require.NoError(t, err)
// If any ticket is lost in any part of a match, all matched tickets will not be assigned.
err = backend.assign(ctx, matches)
require.NoError(t, err)
_, err = frontStore.GetAssignment(ctx, "t1")
require.ErrorIs(t, err, statestore.ErrAssignmentNotFound)
_, err = frontStore.GetAssignment(ctx, "t2")
require.ErrorIs(t, err, statestore.ErrAssignmentNotFound)
// Any remaining tickets for which no assignment is made will return to active again.
activeTickets, err = backend.fetchActiveTickets(ctx, defaultFetchTicketsLimit)
require.NoError(t, err)
require.Len(t, activeTickets, 1)
require.Equal(t, "t2", activeTickets[0].Id)
})
t.Run("ValidationDisabled", func(t *testing.T) {
backend, err := NewBackend(backStore, AssignerFunc(dummyAssign), WithTicketValidationBeforeAssign(false))
require.NoError(t, err)
backend.AddMatchFunction(anyProfile, MatchFunctionSimple1vs1)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t3"}, defaultTicketTTL)
require.NoError(t, err)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t4"}, defaultTicketTTL)
require.NoError(t, err)
activeTickets, err := backend.fetchActiveTickets(ctx, defaultFetchTicketsLimit)
require.NoError(t, err)
require.Len(t, activeTickets, 2)
matches, err := backend.makeMatches(ctx, activeTickets)
require.NoError(t, err)
require.Len(t, matches, 1)
// Delete "t3" after match is established
err = frontStore.DeleteTicket(ctx, "t3")
require.NoError(t, err)
// Assignment is created even if the ticket disappears because the validation is invalid.
err = backend.assign(ctx, matches)
require.NoError(t, err)
as1, err := frontStore.GetAssignment(ctx, "t3")
require.NoError(t, err)
require.NotNil(t, as1)
as2, err := frontStore.GetAssignment(ctx, "t4")
require.NoError(t, err)
require.NotNil(t, as2)
require.Equal(t, as1.Connection, as2.Connection)
activeTickets, err = backend.fetchActiveTickets(ctx, defaultFetchTicketsLimit)
require.NoError(t, err)
require.Empty(t, activeTickets)
})
}
func TestGracefulShutdown(t *testing.T) {
frontStore, backStore, _ := NewStateStoreWithMiniRedis(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
backend, err := NewBackend(backStore, AssignerFunc(func(ctx context.Context, matches []*pb.Match) ([]*pb.AssignmentGroup, error) {
time.Sleep(500 * time.Millisecond)
return dummyAssign(ctx, matches)
}))
require.NoError(t, err)
backend.AddMatchFunction(anyProfile, MatchFunctionSimple1vs1)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t1"}, defaultTicketTTL)
require.NoError(t, err)
err = frontStore.CreateTicket(ctx, &pb.Ticket{Id: "t2"}, defaultTicketTTL)
require.NoError(t, err)
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
return backend.Start(egCtx, 10*time.Millisecond)
})
time.Sleep(50 * time.Millisecond)
cancel() // stop backend
require.ErrorIs(t, eg.Wait(), context.Canceled)
// Even if backend stops, the tick being processed will be completed.
ctx = context.Background()
as1, err := frontStore.GetAssignment(ctx, "t1")
require.NoError(t, err)
require.NotNil(t, as1)
as2, err := frontStore.GetAssignment(ctx, "t2")
require.NoError(t, err)
require.NotNil(t, as2)
require.Equal(t, as1.Connection, as2.Connection)
}
var anyProfile = &pb.MatchProfile{
Name: "test-profile",
Pools: []*pb.Pool{
{Name: "test-pool"},
},
}
func dummyAssign(ctx context.Context, matches []*pb.Match) ([]*pb.AssignmentGroup, error) {
var asgs []*pb.AssignmentGroup
for _, match := range matches {
tids := ticketIDsFromMatch(match)
conn := hri.Random()
log.Printf("assign '%s' to tickets: %v", conn, tids)
asgs = append(asgs, &pb.AssignmentGroup{
TicketIds: tids,
Assignment: &pb.Assignment{Connection: conn},
})
}
return asgs, nil
}
func ticketIDsFromMatch(match *pb.Match) []string {
var ids []string
for _, ticket := range match.Tickets {
ids = append(ids, ticket.Id)
}
return ids
}