Skip to content

Commit

Permalink
dialer mock with delay option
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaldekloe committed Aug 15, 2024
1 parent 25ddcda commit 3f0a464
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func newClientPipeN(t *testing.T, n int, want ...mqtttest.Transfer) (*mqtt.Clien
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 2,
ExactlyOnceMax: 2,
Dialer: newTestDialer(t, clientConns...),
Dialer: newDialerMock(t, 0, clientConns...),
})
if err != nil {
t.Fatal("volatile session error:", err)
Expand All @@ -176,9 +176,9 @@ func newClientPipeN(t *testing.T, n int, want ...mqtttest.Transfer) (*mqtt.Clien

var errLastTestConn = errors.New("Dialer mock exhausted: all connections served")

// NewTestDialer returns a new dialer which returns the conns in order of
// NewDialerMock returns a new dialer which returns the conns in order of
// appearance. The test fails on fewer dials.
func newTestDialer(t *testing.T, conns ...net.Conn) mqtt.Dialer {
func newDialerMock(t *testing.T, delay time.Duration, conns ...net.Conn) mqtt.Dialer {
t.Helper()

var dialN atomic.Uint64
Expand All @@ -196,6 +196,8 @@ func newTestDialer(t *testing.T, conns ...net.Conn) mqtt.Dialer {
if n > uint64(len(conns)) {
return nil, errLastTestConn
}

time.Sleep(delay)
return conns[n-1], nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func TestPublishAtLeastOnceRestart(t *testing.T) {
client, err := mqtt.InitSession("test-client", mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 3,
Dialer: newTestDialer(t, clientConn),
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("InitSession error:", err)
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestPublishAtLeastOnceRestart(t *testing.T) {
client, warn, err := mqtt.AdoptSession(mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 3,
Dialer: newTestDialer(t, clientConn),
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("AdoptSession error:", err)
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestPublishExactlyOnceRestart(t *testing.T) {
client, err := mqtt.InitSession("test-client", mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
ExactlyOnceMax: 5,
Dialer: newTestDialer(t, clientConn),
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("InitSession error:", err)
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestPublishExactlyOnceRestart(t *testing.T) {
client, warn, err := mqtt.AdoptSession(mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
ExactlyOnceMax: 4,
Dialer: newTestDialer(t, clientConn),
Dialer: newDialerMock(t, 0, clientConn),
})
for _, err := range warn {
t.Error("AdoptSession warning:", err)
Expand Down

0 comments on commit 3f0a464

Please sign in to comment.