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