Skip to content

Commit 889458a

Browse files
wszaranskialicebob
authored andcommitted
Move create of client in tests to helper function
Removed duplicated code in tests using helper function. Signed-off-by: Wojciech Szarański <wojciech.szaranski@gmail.com>
1 parent 0dd44a9 commit 889458a

21 files changed

+218
-778
lines changed

cmd_client_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
// Test CLIENT *.
1010
func TestClient(t *testing.T) {
1111
t.Run("setname and getname", func(t *testing.T) {
12-
s := RunT(t)
13-
c, err := proto.Dial(s.Addr())
14-
ok(t, err)
15-
defer c.Close()
12+
_, c := runWithClient(t)
1613

1714
// Set the client name
1815
mustDo(t, c,
@@ -28,10 +25,7 @@ func TestClient(t *testing.T) {
2825
})
2926

3027
t.Run("getname without setname", func(t *testing.T) {
31-
s := RunT(t)
32-
c, err := proto.Dial(s.Addr())
33-
ok(t, err)
34-
defer c.Close()
28+
_, c := runWithClient(t)
3529

3630
// Get the client name without setting it first
3731
mustDo(t, c,

cmd_cluster_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99

1010
// Test CLUSTER *.
1111
func TestCluster(t *testing.T) {
12-
s := RunT(t)
13-
c, err := proto.Dial(s.Addr())
14-
ok(t, err)
15-
defer c.Close()
12+
s, c := runWithClient(t)
1613

1714
t.Run("slots", func(t *testing.T) {
1815
port, err := strconv.Atoi(s.Port())

cmd_connection_test.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import (
88

99
func TestAuth(t *testing.T) {
1010
t.Run("default user", func(t *testing.T) {
11-
s := RunT(t)
12-
c, err := proto.Dial(s.Addr())
13-
ok(t, err)
14-
defer c.Close()
11+
s, c := runWithClient(t)
1512

1613
mustDo(t, c,
1714
"AUTH", "foo", "bar", "baz",
@@ -38,10 +35,7 @@ func TestAuth(t *testing.T) {
3835
})
3936

4037
t.Run("another user", func(t *testing.T) {
41-
s := RunT(t)
42-
c, err := proto.Dial(s.Addr())
43-
ok(t, err)
44-
defer c.Close()
38+
s, c := runWithClient(t)
4539

4640
s.RequireUserAuth("hello", "world")
4741
mustDo(t, c,
@@ -67,10 +61,7 @@ func TestAuth(t *testing.T) {
6761
})
6862

6963
t.Run("error cases", func(t *testing.T) {
70-
s := RunT(t)
71-
c, err := proto.Dial(s.Addr())
72-
ok(t, err)
73-
defer c.Close()
64+
_, c := runWithClient(t)
7465

7566
mustDo(t, c,
7667
"AUTH",
@@ -85,10 +76,7 @@ func TestAuth(t *testing.T) {
8576
}
8677

8778
func TestPing(t *testing.T) {
88-
s := RunT(t)
89-
c, err := proto.Dial(s.Addr())
90-
ok(t, err)
91-
defer c.Close()
79+
_, c := runWithClient(t)
9280

9381
t.Run("no args", func(t *testing.T) {
9482
mustDo(t, c,
@@ -113,10 +101,7 @@ func TestPing(t *testing.T) {
113101
}
114102

115103
func TestEcho(t *testing.T) {
116-
s := RunT(t)
117-
c, err := proto.Dial(s.Addr())
118-
ok(t, err)
119-
defer c.Close()
104+
_, c := runWithClient(t)
120105

121106
mustDo(t, c,
122107
"ECHO", "hello\nworld",
@@ -130,10 +115,7 @@ func TestEcho(t *testing.T) {
130115
}
131116

132117
func TestSelect(t *testing.T) {
133-
s := RunT(t)
134-
c, err := proto.Dial(s.Addr())
135-
ok(t, err)
136-
defer c.Close()
118+
s, c := runWithClient(t)
137119

138120
mustOK(t, c, "SET", "foo", "bar")
139121
mustOK(t, c, "SELECT", "5")
@@ -161,10 +143,7 @@ func TestSelect(t *testing.T) {
161143
}
162144

163145
func TestSwapdb(t *testing.T) {
164-
s := RunT(t)
165-
c, err := proto.Dial(s.Addr())
166-
ok(t, err)
167-
defer c.Close()
146+
s, c := runWithClient(t)
168147

169148
mustOK(t, c, "SET", "foo", "bar")
170149
mustOK(t, c, "SELECT", "5")
@@ -224,10 +203,7 @@ func TestSwapdb(t *testing.T) {
224203
}
225204

226205
func TestQuit(t *testing.T) {
227-
s := RunT(t)
228-
c, err := proto.Dial(s.Addr())
229-
ok(t, err)
230-
defer c.Close()
206+
_, c := runWithClient(t)
231207

232208
mustOK(t, c, "QUIT")
233209

@@ -237,10 +213,7 @@ func TestQuit(t *testing.T) {
237213
}
238214

239215
func TestSetError(t *testing.T) {
240-
s := RunT(t)
241-
c, err := proto.Dial(s.Addr())
242-
ok(t, err)
243-
defer c.Close()
216+
s, c := runWithClient(t)
244217

245218
mustDo(t, c,
246219
"PING",
@@ -262,10 +235,7 @@ func TestSetError(t *testing.T) {
262235

263236
func TestHello(t *testing.T) {
264237
t.Run("default user", func(t *testing.T) {
265-
s := RunT(t)
266-
c, err := proto.Dial(s.Addr())
267-
ok(t, err)
268-
defer c.Close()
238+
s, c := runWithClient(t)
269239

270240
payl := proto.Map(
271241
proto.String("server"), proto.String("miniredis"),

cmd_generic_test.go

Lines changed: 18 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import (
1010

1111
// Test EXPIRE. Keys with an expiration are called volatile in Redis parlance.
1212
func TestTTL(t *testing.T) {
13-
s := RunT(t)
14-
c, err := proto.Dial(s.Addr())
15-
ok(t, err)
16-
defer c.Close()
13+
s, c := runWithClient(t)
1714

1815
t.Run("parse", func(t *testing.T) {
1916
t.Run("basic", func(t *testing.T) {
@@ -102,10 +99,7 @@ func TestTTL(t *testing.T) {
10299
}
103100

104101
func TestExpireat(t *testing.T) {
105-
s := RunT(t)
106-
c, err := proto.Dial(s.Addr())
107-
ok(t, err)
108-
defer c.Close()
102+
s, c := runWithClient(t)
109103

110104
// Not volatile yet
111105
{
@@ -136,10 +130,7 @@ func TestExpireat(t *testing.T) {
136130
}
137131

138132
func TestTouch(t *testing.T) {
139-
s := RunT(t)
140-
c, err := proto.Dial(s.Addr())
141-
ok(t, err)
142-
defer c.Close()
133+
s, c := runWithClient(t)
143134

144135
// Set something
145136
t.Run("basic", func(t *testing.T) {
@@ -176,10 +167,7 @@ func TestTouch(t *testing.T) {
176167
}
177168

178169
func TestPexpireat(t *testing.T) {
179-
s := RunT(t)
180-
c, err := proto.Dial(s.Addr())
181-
ok(t, err)
182-
defer c.Close()
170+
s, c := runWithClient(t)
183171

184172
// Not volatile yet
185173
{
@@ -212,10 +200,7 @@ func TestPexpireat(t *testing.T) {
212200
}
213201

214202
func TestPexpire(t *testing.T) {
215-
s := RunT(t)
216-
c, err := proto.Dial(s.Addr())
217-
ok(t, err)
218-
defer c.Close()
203+
s, c := runWithClient(t)
219204

220205
t.Run("key exists", func(t *testing.T) {
221206
ok(t, s.Set("foo", "bar"))
@@ -246,10 +231,7 @@ func TestPexpire(t *testing.T) {
246231
}
247232

248233
func TestDel(t *testing.T) {
249-
s := RunT(t)
250-
c, err := proto.Dial(s.Addr())
251-
ok(t, err)
252-
defer c.Close()
234+
s, c := runWithClient(t)
253235

254236
t.Run("simple", func(t *testing.T) {
255237
s.Set("foo", "bar")
@@ -281,10 +263,7 @@ func TestDel(t *testing.T) {
281263
}
282264

283265
func TestUnlink(t *testing.T) {
284-
s := RunT(t)
285-
c, err := proto.Dial(s.Addr())
286-
ok(t, err)
287-
defer c.Close()
266+
s, c := runWithClient(t)
288267

289268
t.Run("simple", func(t *testing.T) {
290269
s.Set("foo", "bar")
@@ -309,10 +288,7 @@ func TestUnlink(t *testing.T) {
309288
}
310289

311290
func TestType(t *testing.T) {
312-
s := RunT(t)
313-
c, err := proto.Dial(s.Addr())
314-
ok(t, err)
315-
defer c.Close()
291+
s, c := runWithClient(t)
316292

317293
s.Set("foo", "bar!")
318294
t.Run("string", func(t *testing.T) {
@@ -355,10 +331,7 @@ func TestType(t *testing.T) {
355331
}
356332

357333
func TestExpireTime(t *testing.T) {
358-
s := RunT(t)
359-
c, err := proto.Dial(s.Addr())
360-
ok(t, err)
361-
defer c.Close()
334+
s, c := runWithClient(t)
362335

363336
t.Run("nosuch", func(t *testing.T) {
364337
mustDo(t, c, "EXPIRETIME", "nosuch", proto.Int(-2))
@@ -379,10 +352,7 @@ func TestExpireTime(t *testing.T) {
379352
}
380353

381354
func TestPExpireTime(t *testing.T) {
382-
s := RunT(t)
383-
c, err := proto.Dial(s.Addr())
384-
ok(t, err)
385-
defer c.Close()
355+
s, c := runWithClient(t)
386356

387357
t.Run("nosuch", func(t *testing.T) {
388358
mustDo(t, c, "PEXPIRETIME", "nosuch", proto.Int(-2))
@@ -403,10 +373,7 @@ func TestPExpireTime(t *testing.T) {
403373
}
404374

405375
func TestExists(t *testing.T) {
406-
s := RunT(t)
407-
c, err := proto.Dial(s.Addr())
408-
ok(t, err)
409-
defer c.Close()
376+
s, c := runWithClient(t)
410377

411378
t.Run("string", func(t *testing.T) {
412379
s.Set("foo", "bar!")
@@ -448,10 +415,7 @@ func TestExists(t *testing.T) {
448415
}
449416

450417
func TestMove(t *testing.T) {
451-
s := RunT(t)
452-
c, err := proto.Dial(s.Addr())
453-
ok(t, err)
454-
defer c.Close()
418+
s, c := runWithClient(t)
455419

456420
// No problem.
457421
{
@@ -501,10 +465,7 @@ func TestMove(t *testing.T) {
501465
}
502466

503467
func TestKeys(t *testing.T) {
504-
s := RunT(t)
505-
c, err := proto.Dial(s.Addr())
506-
ok(t, err)
507-
defer c.Close()
468+
s, c := runWithClient(t)
508469

509470
s.Set("foo", "bar!")
510471
s.Set("foobar", "bar!")
@@ -547,10 +508,7 @@ func TestKeys(t *testing.T) {
547508
}
548509

549510
func TestRandom(t *testing.T) {
550-
s := RunT(t)
551-
c, err := proto.Dial(s.Addr())
552-
ok(t, err)
553-
defer c.Close()
511+
s, c := runWithClient(t)
554512

555513
// Empty db.
556514
mustNil(t, c, "RANDOMKEY")
@@ -575,10 +533,7 @@ func TestRandom(t *testing.T) {
575533
}
576534

577535
func TestRename(t *testing.T) {
578-
s := RunT(t)
579-
c, err := proto.Dial(s.Addr())
580-
ok(t, err)
581-
defer c.Close()
536+
s, c := runWithClient(t)
582537

583538
// Non-existing key
584539
mustDo(t, c,
@@ -652,10 +607,7 @@ func TestRename(t *testing.T) {
652607
}
653608

654609
func TestScan(t *testing.T) {
655-
s := RunT(t)
656-
c, err := proto.Dial(s.Addr())
657-
ok(t, err)
658-
defer c.Close()
610+
s, c := runWithClient(t)
659611

660612
t.Run("parse", func(t *testing.T) {
661613
t.Run("basic", func(t *testing.T) {
@@ -816,10 +768,7 @@ func TestScan(t *testing.T) {
816768
}
817769

818770
func TestRenamenx(t *testing.T) {
819-
s := RunT(t)
820-
c, err := proto.Dial(s.Addr())
821-
ok(t, err)
822-
defer c.Close()
771+
s, c := runWithClient(t)
823772

824773
// Non-existing key
825774
mustDo(t, c,
@@ -871,10 +820,7 @@ func TestRenamenx(t *testing.T) {
871820
}
872821

873822
func TestCopy(t *testing.T) {
874-
s := RunT(t)
875-
c, err := proto.Dial(s.Addr())
876-
ok(t, err)
877-
defer c.Close()
823+
s, c := runWithClient(t)
878824

879825
t.Run("parse", func(t *testing.T) {
880826
t.Run("basic", func(t *testing.T) {

0 commit comments

Comments
 (0)