Skip to content

Commit 344cbde

Browse files
committed
pss/outbox: outbox_tests, now using Epic's testing framework (ut)
1 parent 54c0609 commit 344cbde

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

pss/outbox/outbox.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type outbox struct {
2929

3030
type forwardFunction func(msg *message.Message) error
3131

32+
var ErrOutboxFull = errors.New("outbox full")
33+
3234
func NewOutbox(config *Config) *outbox {
3335
outbox := outbox{
3436
forwardFunc: config.Forward,
@@ -60,7 +62,7 @@ func (o *outbox) Enqueue(outboxMsg *outboxMsg) error {
6062
return nil
6163
default:
6264
metrics.GetOrRegisterCounter("pss.enqueue.outbox.full", nil).Inc(1)
63-
return errors.New("outbox full")
65+
return ErrOutboxFull
6466
}
6567
}
6668

pss/outbox/outbox_test.go

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package outbox
22

33
import (
44
"errors"
5-
"github.com/ethersphere/swarm/pss/message"
65
"testing"
76
"time"
7+
8+
"github.com/epiclabs-io/ut"
9+
"github.com/ethersphere/swarm/pss/message"
810
)
911

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()
1117

1218
outboxCapacity := 2
1319
successC := make(chan struct{})
@@ -21,22 +27,17 @@ func TestMessageOutbox(t *testing.T) {
2127
QuitC: nil,
2228
Forward: mockForwardFunction,
2329
})
24-
2530
go testOutbox.ProcessOutbox()
2631

2732
err := testOutbox.Enqueue(testOutboxMessage)
28-
if err != nil {
29-
t.Fatalf("expected no error, got %v", err)
30-
}
33+
t.Ok(err)
3134

3235
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)
3637

3738
select {
3839
case <-successC:
39-
case <-time.After(2 * time.Second):
40+
case <-time.After(waitTimeout):
4041
t.Fatal("timeout waiting for success forward")
4142
}
4243

@@ -49,39 +50,39 @@ func TestMessageOutbox(t *testing.T) {
4950
<-continueC
5051
return errors.New("forced test error forwarding message")
5152
}
52-
5353
testOutbox.forwardFunc = failedForward
5454

5555
err = testOutbox.Enqueue(testOutboxMessage)
56-
if err != nil {
57-
t.Fatalf("Expected no error enqueing, got %v", err.Error())
58-
}
56+
t.Ok(err)
5957

6058
select {
6159
case <-failedC:
62-
case <-time.After(2 * time.Second):
60+
case <-time.After(waitTimeout):
6361
t.Fatal("timeout waiting for failing forward")
6462
}
6563

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+
6967
// The message will be retried once we send to continueC, so first, we change the forward function
7068
testOutbox.forwardFunc = mockForwardFunction
7169
continueC <- struct{}{}
70+
7271
select {
7372
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")
7675
}
7776
}
7877

79-
func TestOutboxFull(t *testing.T) {
78+
func TestOutboxFull(tx *testing.T) {
79+
t := ut.BeginTest(tx, false)
80+
defer t.FinishTest()
8081

8182
outboxCapacity := 2
82-
procChan := make(chan struct{})
83+
processC := make(chan struct{})
8384
successForward := func(msg *message.Message) error {
84-
<-procChan
85+
<-processC
8586
return nil
8687
}
8788

@@ -90,28 +91,24 @@ func TestOutboxFull(t *testing.T) {
9091
QuitC: nil,
9192
Forward: successForward,
9293
})
93-
9494
go testOutbox.ProcessOutbox()
9595

9696
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+
10099
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)
104101

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
106103
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+
111108
//There should be a slot again in the outbox
112109
select {
113110
case <-testOutbox.slots:
114-
case <-time.After(2 * time.Second):
111+
case <-time.After(waitTimeout):
115112
t.Fatalf("timeout waiting for a free slot")
116113
}
117114
}

0 commit comments

Comments
 (0)