-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathblocking_client_group_test_case.go
More file actions
77 lines (65 loc) · 2.54 KB
/
blocking_client_group_test_case.go
File metadata and controls
77 lines (65 loc) · 2.54 KB
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
package test_cases
import (
"time"
"github.com/codecrafters-io/redis-tester/internal/instrumented_resp_connection"
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"
"github.com/codecrafters-io/tester-utils/logger"
)
type clientWithExpectedResponse struct {
Client *instrumented_resp_connection.InstrumentedRespConnection
Command string
Args []string
// If nil, we expect no response
Assertion *resp_assertions.RESPAssertion
}
type BlockingClientGroupTestCase struct {
clientsWithExpectedResponses []clientWithExpectedResponse
}
func (t *BlockingClientGroupTestCase) AddClientWithExpectedResponse(client *instrumented_resp_connection.InstrumentedRespConnection, command string, args []string, assertion resp_assertions.RESPAssertion) *BlockingClientGroupTestCase {
t.clientsWithExpectedResponses = append(t.clientsWithExpectedResponses, clientWithExpectedResponse{
Client: client,
Command: command,
Args: args,
Assertion: &assertion,
})
return t
}
func (t *BlockingClientGroupTestCase) AddClientWithNoExpectedResponse(client *instrumented_resp_connection.InstrumentedRespConnection, command string, args []string) *BlockingClientGroupTestCase {
t.clientsWithExpectedResponses = append(t.clientsWithExpectedResponses, clientWithExpectedResponse{
Client: client,
Command: command,
Args: args,
Assertion: nil,
})
return t
}
func (t *BlockingClientGroupTestCase) SendBlockingCommands() error {
for _, clientWithExpectedResponse := range t.clientsWithExpectedResponses {
if err := clientWithExpectedResponse.Client.SendCommand(clientWithExpectedResponse.Command, clientWithExpectedResponse.Args...); err != nil {
return err
}
time.Sleep(1 * time.Millisecond) // Ensure server receives commands in order
}
return nil
}
func (t *BlockingClientGroupTestCase) AssertResponses(logger *logger.Logger) error {
for i := len(t.clientsWithExpectedResponses) - 1; i >= 0; i-- {
clientWithExpectedResponse := t.clientsWithExpectedResponses[i]
clientLogger := clientWithExpectedResponse.Client.GetLogger()
if clientWithExpectedResponse.Assertion == nil {
testCase := NoResponseTestCase{}
if err := testCase.Run(clientWithExpectedResponse.Client); err != nil {
return err
}
} else {
clientLogger.Infof("Expecting response of %s command", clientWithExpectedResponse.Command)
testCase := ReceiveValueTestCase{
Assertion: *clientWithExpectedResponse.Assertion,
}
if err := testCase.Run(clientWithExpectedResponse.Client, logger); err != nil {
return err
}
}
}
return nil
}