Skip to content

Commit 049b17f

Browse files
committed
misc: refactor loop tests to use require where possible
1 parent bdb4b77 commit 049b17f

10 files changed

+99
-192
lines changed

client_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package loop
22

33
import (
4-
"bytes"
54
"context"
65
"crypto/sha256"
76
"errors"
@@ -57,9 +56,7 @@ func TestLoopOutSuccess(t *testing.T) {
5756

5857
// Initiate loop out.
5958
info, err := ctx.swapClient.LoopOut(context.Background(), &req)
60-
if err != nil {
61-
t.Fatal(err)
62-
}
59+
require.NoError(t, err)
6360

6461
ctx.assertStored()
6562
ctx.assertStatus(loopdb.StateInitiated)
@@ -84,9 +81,7 @@ func TestLoopOutFailOffchain(t *testing.T) {
8481
ctx := createClientTestContext(t, nil)
8582

8683
_, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
87-
if err != nil {
88-
t.Fatal(err)
89-
}
84+
require.NoError(t, err)
9085

9186
ctx.assertStored()
9287
ctx.assertStatus(loopdb.StateInitiated)
@@ -208,14 +203,10 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
208203
amt := btcutil.Amount(50000)
209204

210205
swapPayReq, err := getInvoice(hash, amt, swapInvoiceDesc)
211-
if err != nil {
212-
t.Fatal(err)
213-
}
206+
require.NoError(t, err)
214207

215208
prePayReq, err := getInvoice(hash, 100, prepayInvoiceDesc)
216-
if err != nil {
217-
t.Fatal(err)
218-
}
209+
require.NoError(t, err)
219210

220211
_, senderPubKey := test.CreateKey(1)
221212
var senderKey [33]byte
@@ -373,10 +364,11 @@ func testLoopOutSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
373364
// Expect client on-chain sweep of HTLC.
374365
sweepTx := ctx.ReceiveTx()
375366

376-
if !bytes.Equal(sweepTx.TxIn[0].PreviousOutPoint.Hash[:],
377-
htlcOutpoint.Hash[:]) {
378-
ctx.T.Fatalf("client not sweeping from htlc tx")
379-
}
367+
require.Equal(
368+
ctx.T, htlcOutpoint.Hash[:],
369+
sweepTx.TxIn[0].PreviousOutPoint.Hash[:],
370+
"client not sweeping from htlc tx",
371+
)
380372

381373
var preImageIndex int
382374
switch scriptVersion {
@@ -390,9 +382,7 @@ func testLoopOutSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
390382
// Check preimage.
391383
clientPreImage := sweepTx.TxIn[0].Witness[preImageIndex]
392384
clientPreImageHash := sha256.Sum256(clientPreImage)
393-
if clientPreImageHash != hash {
394-
ctx.T.Fatalf("incorrect preimage")
395-
}
385+
require.Equal(ctx.T, hash, lntypes.Hash(clientPreImageHash))
396386

397387
// Since we successfully published our sweep, we expect the preimage to
398388
// have been pushed to our mock server.

loopd/swapclient_server_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,13 @@ func TestValidateConfTarget(t *testing.T) {
130130
test.confTarget, defaultConf,
131131
)
132132

133-
haveErr := err != nil
134-
if haveErr != test.expectErr {
135-
t.Fatalf("expected err: %v, got: %v",
136-
test.expectErr, err)
133+
if test.expectErr {
134+
require.Error(t, err)
135+
} else {
136+
require.NoError(t, err)
137137
}
138138

139-
if target != test.expectedTarget {
140-
t.Fatalf("expected: %v, got: %v",
141-
test.expectedTarget, target)
142-
}
139+
require.Equal(t, test.expectedTarget, target)
143140
})
144141
}
145142
}
@@ -199,16 +196,13 @@ func TestValidateLoopInRequest(t *testing.T) {
199196
test.confTarget, external,
200197
)
201198

202-
haveErr := err != nil
203-
if haveErr != test.expectErr {
204-
t.Fatalf("expected err: %v, got: %v",
205-
test.expectErr, err)
199+
if test.expectErr {
200+
require.Error(t, err)
201+
} else {
202+
require.NoError(t, err)
206203
}
207204

208-
if conf != test.expectedTarget {
209-
t.Fatalf("expected: %v, got: %v",
210-
test.expectedTarget, conf)
211-
}
205+
require.Equal(t, test.expectedTarget, conf)
212206
})
213207
}
214208
}

loopin_test.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ func testLoopInSuccess(t *testing.T) {
5858
context.Background(), cfg,
5959
height, req,
6060
)
61-
if err != nil {
62-
t.Fatal(err)
63-
}
61+
require.NoError(t, err)
62+
6463
inSwap := initResult.swap
6564

6665
ctx.store.assertLoopInStored()
@@ -142,10 +141,7 @@ func testLoopInSuccess(t *testing.T) {
142141
ctx.assertState(loopdb.StateSuccess)
143142
ctx.store.assertLoopInState(loopdb.StateSuccess)
144143

145-
err = <-errChan
146-
if err != nil {
147-
t.Fatal(err)
148-
}
144+
require.NoError(t, <-errChan)
149145
}
150146

151147
// TestLoopInTimeout tests scenarios where the server doesn't sweep the htlc
@@ -215,9 +211,7 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
215211
context.Background(), cfg,
216212
height, &req,
217213
)
218-
if err != nil {
219-
t.Fatal(err)
220-
}
214+
require.NoError(t, err)
221215
inSwap := initResult.swap
222216

223217
ctx.store.assertLoopInStored()
@@ -289,11 +283,7 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
289283
ctx.assertState(loopdb.StateFailIncorrectHtlcAmt)
290284
ctx.store.assertLoopInState(loopdb.StateFailIncorrectHtlcAmt)
291285

292-
err = <-errChan
293-
if err != nil {
294-
t.Fatal(err)
295-
}
296-
286+
require.NoError(t, <-errChan)
297287
return
298288
}
299289

@@ -308,9 +298,11 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
308298

309299
// Expect a signing request for the htlc tx output value.
310300
signReq := <-ctx.lnd.SignOutputRawChannel
311-
if signReq.SignDescriptors[0].Output.Value != htlcTx.TxOut[0].Value {
312-
t.Fatal("invalid signing amount")
313-
}
301+
require.Equal(
302+
t, htlcTx.TxOut[0].Value,
303+
signReq.SignDescriptors[0].Output.Value,
304+
"invalid signing amount",
305+
)
314306

315307
// Expect timeout tx to be published.
316308
timeoutTx := <-ctx.lnd.TxPublishChannel
@@ -341,10 +333,7 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
341333
state := ctx.store.assertLoopInState(loopdb.StateFailTimeout)
342334
require.Equal(t, cost, state.Cost)
343335

344-
err = <-errChan
345-
if err != nil {
346-
t.Fatal(err)
347-
}
336+
require.NoError(t, <-errChan)
348337
}
349338

350339
// TestLoopInResume tests resuming swaps in various states.
@@ -483,17 +472,10 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
483472
require.NoError(t, err)
484473

485474
err = ctx.store.CreateLoopIn(testPreimage.Hash(), contract)
486-
if err != nil {
487-
t.Fatal(err)
488-
}
475+
require.NoError(t, err)
489476

490-
inSwap, err := resumeLoopInSwap(
491-
context.Background(), cfg,
492-
pendSwap,
493-
)
494-
if err != nil {
495-
t.Fatal(err)
496-
}
477+
inSwap, err := resumeLoopInSwap(context.Background(), cfg, pendSwap)
478+
require.NoError(t, err)
497479

498480
var height int32
499481
if expired {
@@ -512,10 +494,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
512494
}()
513495

514496
defer func() {
515-
err = <-errChan
516-
if err != nil {
517-
t.Fatal(err)
518-
}
497+
require.NoError(t, <-errChan)
519498

520499
select {
521500
case <-ctx.lnd.SendPaymentChannel:

loopin_testcontext_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ func newLoopInTestContext(t *testing.T) *loopInTestContext {
6363

6464
func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
6565
state := <-c.statusChan
66-
if state.State != expectedState {
67-
c.t.Fatalf("expected state %v but got %v", expectedState,
68-
state.State)
69-
}
66+
require.Equal(c.t, expectedState, state.State)
7067
}
7168

7269
// assertSubscribeInvoice asserts that the client subscribes to invoice updates

0 commit comments

Comments
 (0)