-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
92 lines (76 loc) · 2.14 KB
/
main_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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
go Start()
// wait of the server to be ready
time.Sleep(time.Second)
code := m.Run()
os.Exit(code)
}
func TestSchemeSimulator(t *testing.T) {
tests := []struct {
name string
input string
expectedOutput string
minDuration time.Duration
maxDuration time.Duration
}{
{
name: "Valid Request",
input: "PAYMENT|10",
expectedOutput: "RESPONSE|ACCEPTED|Transaction processed",
maxDuration: 50 * time.Millisecond,
},
{
name: "Valid Request with Delay",
input: "PAYMENT|101",
expectedOutput: "RESPONSE|ACCEPTED|Transaction processed",
minDuration: 101 * time.Millisecond,
maxDuration: 151 * time.Millisecond,
},
{
name: "Invalid Request Format",
input: "INVALID|100",
expectedOutput: "RESPONSE|REJECTED|Invalid request",
maxDuration: 10 * time.Millisecond,
},
{
name: "Large Amount",
input: "PAYMENT|20000",
expectedOutput: "RESPONSE|ACCEPTED|Transaction processed",
minDuration: 10 * time.Second,
maxDuration: 10*time.Second + 50*time.Millisecond,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conn, err := net.Dial("tcp", ":8080")
require.NoError(t, err, "Failed to connect to server")
defer conn.Close()
_, err = fmt.Fprintf(conn, tt.input+"\n")
require.NoError(t, err, "Failed to send request")
start := time.Now()
response, err := bufio.NewReader(conn).ReadString('\n')
require.NoError(t, err, "Failed to read response")
duration := time.Since(start)
response = strings.TrimSpace(response)
assert.Equal(t, tt.expectedOutput, response, "Unexpected response")
if tt.minDuration > 0 {
assert.GreaterOrEqual(t, duration, tt.minDuration, "Response time was shorter than expected")
}
if tt.maxDuration > 0 {
assert.LessOrEqual(t, duration, tt.maxDuration, "Response time was longer than expected")
}
})
}
}