@@ -2,12 +2,18 @@ package outbox
2
2
3
3
import (
4
4
"errors"
5
- "github.com/ethersphere/swarm/pss/message"
6
5
"testing"
7
6
"time"
7
+
8
+ "github.com/epiclabs-io/ut"
9
+ "github.com/ethersphere/swarm/pss/message"
8
10
)
9
11
10
- func TestMessageOutbox (t * testing.T ) {
12
+ const waitTimeout = 2 * time .Second
13
+
14
+ func TestOutbox (tx * testing.T ) {
15
+ t := ut .BeginTest (tx , false )
16
+ defer t .FinishTest ()
11
17
12
18
outboxCapacity := 2
13
19
successC := make (chan struct {})
@@ -21,22 +27,17 @@ func TestMessageOutbox(t *testing.T) {
21
27
QuitC : nil ,
22
28
Forward : mockForwardFunction ,
23
29
})
24
-
25
30
go testOutbox .ProcessOutbox ()
26
31
27
32
err := testOutbox .Enqueue (testOutboxMessage )
28
- if err != nil {
29
- t .Fatalf ("expected no error, got %v" , err )
30
- }
33
+ t .Ok (err )
31
34
32
35
usedSlots := testOutbox .len ()
33
- if usedSlots != 1 {
34
- t .Fatalf ("incorrect outbox length. expected 1, got %v" , usedSlots )
35
- }
36
+ t .Assert (usedSlots == 1 , "incorrect outbox length. expected 1, got %v" , usedSlots )
36
37
37
38
select {
38
39
case <- successC :
39
- case <- time .After (2 * time . Second ):
40
+ case <- time .After (waitTimeout ):
40
41
t .Fatal ("timeout waiting for success forward" )
41
42
}
42
43
@@ -49,39 +50,39 @@ func TestMessageOutbox(t *testing.T) {
49
50
<- continueC
50
51
return errors .New ("forced test error forwarding message" )
51
52
}
52
-
53
53
testOutbox .forwardFunc = failedForward
54
54
55
55
err = testOutbox .Enqueue (testOutboxMessage )
56
- if err != nil {
57
- t .Fatalf ("Expected no error enqueing, got %v" , err .Error ())
58
- }
56
+ t .Ok (err )
59
57
60
58
select {
61
59
case <- failedC :
62
- case <- time .After (2 * time . Second ):
60
+ case <- time .After (waitTimeout ):
63
61
t .Fatal ("timeout waiting for failing forward" )
64
62
}
65
63
66
- if len (failed ) == 0 {
67
- t . Fatal ( "Incorrect number of failed messages, expected 1 got 0" )
68
- }
64
+ failedMessages := len (failed )
65
+ t . Assert ( failedMessages == 1 , "incorrect number of failed messages, expected 1 got %v" , failedMessages )
66
+
69
67
// The message will be retried once we send to continueC, so first, we change the forward function
70
68
testOutbox .forwardFunc = mockForwardFunction
71
69
continueC <- struct {}{}
70
+
72
71
select {
73
72
case <- successC :
74
- case <- time .After (2 * time . Second ):
75
- t .Fatal ("Timeout waiting for second success forward" )
73
+ case <- time .After (waitTimeout ):
74
+ t .Fatal ("timeout waiting for second success forward" )
76
75
}
77
76
}
78
77
79
- func TestOutboxFull (t * testing.T ) {
78
+ func TestOutboxFull (tx * testing.T ) {
79
+ t := ut .BeginTest (tx , false )
80
+ defer t .FinishTest ()
80
81
81
82
outboxCapacity := 2
82
- procChan := make (chan struct {})
83
+ processC := make (chan struct {})
83
84
successForward := func (msg * message.Message ) error {
84
- <- procChan
85
+ <- processC
85
86
return nil
86
87
}
87
88
@@ -90,28 +91,24 @@ func TestOutboxFull(t *testing.T) {
90
91
QuitC : nil ,
91
92
Forward : successForward ,
92
93
})
93
-
94
94
go testOutbox .ProcessOutbox ()
95
95
96
96
err := testOutbox .Enqueue (testOutboxMessage )
97
- if err != nil {
98
- t .Fatalf ("expected no error enqueing first message, got %v" , err )
99
- }
97
+ t .Ok (err )
98
+
100
99
err = testOutbox .Enqueue (testOutboxMessage )
101
- if err != nil {
102
- t .Fatalf ("expected no error enqueing second message, got %v" , err )
103
- }
100
+ t .Ok (err )
104
101
105
- //As we haven't signaled procChan , the messages are still in the outbox
102
+ //As we haven't signaled processC , the messages are still in the outbox
106
103
err = testOutbox .Enqueue (testOutboxMessage )
107
- if err == nil {
108
- t . Fatalf ( "expected error enqueing third message, instead got nil" )
109
- }
110
- procChan <- struct {}{}
104
+ t . MustFailWith ( err , ErrOutboxFull )
105
+
106
+ processC <- struct {}{ }
107
+
111
108
//There should be a slot again in the outbox
112
109
select {
113
110
case <- testOutbox .slots :
114
- case <- time .After (2 * time . Second ):
111
+ case <- time .After (waitTimeout ):
115
112
t .Fatalf ("timeout waiting for a free slot" )
116
113
}
117
114
}
0 commit comments