-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.sh
More file actions
executable file
·214 lines (191 loc) · 5.67 KB
/
testing.sh
File metadata and controls
executable file
·214 lines (191 loc) · 5.67 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# Setup test environment
make dbtest
if [ $? -ne 0 ]; then
echo "❌ Failed to build dbtest. Exiting."
exit 1
fi
echo "✅ Successfully built dbtest."
make dbserver
if [ $? -ne 0 ]; then
echo "❌ Failed to build dbserver. Exiting."
exit 1
fi
echo "✅ Successfully built dbserver."
rm -rf /tmp/dbtest
mkdir -p /tmp/dbtest
# install tmux if not present
command -v tmux >/dev/null 2>&1 || sudo apt install -y tmux
# start a server in a detached tmux session
tmux new-session -d -s dbserver "./dbserver"
# give enough time for server to start
sleep 2
# Check if the last command failed
check_failure() {
echo "$1" | grep -q '(X)' && return 1 || return 0
}
echo "Running tests..."
# === TEST 1: Write/Read Operation ===
echo ""
echo "=== TEST 1: Write/Read Operations ==="
output1=$(./dbtest -p 5000 -S username "johndoe" 2>&1)
check_failure "$output1"
result1=$?
# echo "Output1: $output1"
output2=$(./dbtest -p 5000 -G username 2>&1)
check_failure "$output2"
result2=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ "$output2" = '="johndoe"' ]; then
echo "✅ Test 1 Passed"
else
echo "❌ Test 1 Failed"
fi
# === TEST 2: Delete Operation ===
echo ""
echo "=== TEST 2: Delete Operation ==="
output1=$(./dbtest -p 5000 -S tempkey "tempvalue" 2>&1)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -D tempkey 2>&1)
check_failure "$output2"
result2=$?
output3=$(./dbtest -p 5000 -G tempkey 2>&1)
check_failure "$output3"
result3=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ $result3 -ne 0 ]; then
echo "✅ Test 2 Passed"
else
echo "❌ Test 2 Failed"
fi
# === TEST 3: Missing Key ===
echo ""
echo "=== TEST 3: Get Missing Key ==="
output1=$(./dbtest -p 5000 -G nonexistentkey 2>&1)
check_failure "$output1"
result1=$?
if [ $result1 -ne 0 ]; then
echo "✅ Test 3 Passed"
else
echo "❌ Test 3 Failed"
fi
# === TEST 4: Overwrite Key ===
echo ""
echo "=== TEST 4: Overwrite Key ==="
output1=$(./dbtest -p 5000 -S overwritekey "value1" 2>&1)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -S overwritekey "value2" 2>&1)
check_failure "$output2"
result2=$?
output3=$(./dbtest -p 5000 -G overwritekey 2>&1)
check_failure "$output3"
result3=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ $result3 -eq 0 ] && [ "$output3" = '="value2"' ]; then
echo "✅ Test 4 Passed"
else
echo "❌ Test 4 Failed"
fi
# === TEST 5: Delete Nonexistent Key ===
echo ""
echo "=== TEST 5: Delete Nonexistent Key ==="
output1=$(./dbtest -p 5000 -D nonexistentkey 2>&1)
check_failure "$output1"
result1=$?
if [ $result1 -ne 0 ]; then
echo "✅ Test 5 Passed"
else
echo "❌ Test 5 Failed"
fi
# === TEST 6: Concurrent Writes ===
echo ""
echo "=== TEST 6: Concurrent Writes ==="
output1=$(./dbtest -p 5000 -S key1 "value1" & ./dbtest -p 5000 -S key2 "value2" & wait)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -G key1 2>&1)
result2=$?
output3=$(./dbtest -p 5000 -G key2 2>&1)
result3=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ $result3 -eq 0 ] && [ "$output2" = '="value1"' ] && [ "$output3" = '="value2"' ]; then
echo "✅ Test 6 Passed"
else
echo "❌ Test 6 Failed"
fi
# === TEST 7: Concurrent Writes On Same Key - Race Condition ===
echo ""
echo "=== TEST 7: Concurrent Writes On Same Key ==="
output1=$(./dbtest -p 5000 -S keySame "value1" & ./dbtest -p 5000 -S keySame "value2" & wait)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -G keySame 2>&1)
result2=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ]; then
echo "✅ Test 7 Passed"
else
echo "❌ Test 7 Failed"
fi
# === TEST 8: 6 Instructions Combination of Read, Write, and Delete ===
echo ""
echo "=== TEST 8: Combination of Read, Write, and Delete ==="
output1=$(./dbtest -p 5000 -S comboKey "initialValue" 2>&1)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -G comboKey 2>&1)
check_failure "$output2"
result2=$?
output3=$(./dbtest -p 5000 -S comboKey "updatedValue" 2>&1)
check_failure "$output3"
result3=$?
output4=$(./dbtest -p 5000 -G comboKey 2>&1)
check_failure "$output4"
result4=$?
output5=$(./dbtest -p 5000 -D comboKey 2>&1)
check_failure "$output5"
result5=$?
output6=$(./dbtest -p 5000 -G comboKey 2>&1)
check_failure "$output6"
result6=$?
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ $result3 -eq 0 ] &&
[ $result4 -eq 0 ] && [ $result5 -eq 0 ] && [ $result6 -ne 0 ]; then
echo "✅ Test 8 Passed"
else
echo "❌ Test 8 Failed"
fi
# === TEST 9: Write and Read 4096 Bytes of Data ===
echo ""
echo "=== TEST 9: Write and Read 4096 Bytes of Data ==="
large_value=$(head -c 4096 </dev/urandom | base64 | head -c 4096)
# generated_length=${#large_value}
# echo "Size of Generated Data: $generated_length bytes"
output1=$(./dbtest -p 5000 -S largeKey "$large_value" 2>&1)
check_failure "$output1"
result1=$?
output2=$(./dbtest -p 5000 -G largeKey 2>&1)
check_failure "$output2"
result2=$?
read_length=${#output2}
# echo "Length of Read Data: $read_length bytes"
if [ $result1 -eq 0 ] && [ $result2 -eq 0 ] && [ $read_length -eq $((4096 + 3)) ]; then # 3 bytes are added for ="" in the output.
echo "✅ Test 9 Passed"
else
echo "❌ Test 9 Failed"
fi
# === TEST 10: Run dbtest with --port, --count, and --threads ===
echo ""
echo "=== TEST 10: Run dbtest with --port, --count, and --threads ==="
output=$(./dbtest --port=5000 --count=100 --threads=5 2>&1 && wait)
result=$?
if [ $result -eq 0 ] && [ -z "$output" ]; then
echo "✅ Test 10 Passed"
else
echo "❌ Test 10 Failed"
fi
# Cleanup
echo ""
echo "All tests completed!"
# Testing completed, send the "quit" command to the server
echo "Stopping the server..."
tmux send-keys -t dbserver "quit" C-m
sleep 2
# kill the tmux session
tmux kill-session -t dbserver