cleanup and refactors
This commit is contained in:
+113
-5
@@ -3,6 +3,7 @@ package honeybee
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.wisehodl.dev/jay/go-honeybee/honeybeetest"
|
||||
"git.wisehodl.dev/jay/go-honeybee/transport"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
@@ -41,7 +42,7 @@ func makeWorker(t *testing.T, ctx context.Context, cancel context.CancelFunc) *D
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
sendHeartbeat: make(chan struct{}),
|
||||
processedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
@@ -134,7 +135,7 @@ func TestWorkerSession(t *testing.T) {
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
sendHeartbeat: make(chan struct{}),
|
||||
processedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
@@ -303,7 +304,7 @@ func TestWorkerSession(t *testing.T) {
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
sendHeartbeat: make(chan struct{}),
|
||||
processedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
@@ -365,7 +366,7 @@ func TestWorkerSession(t *testing.T) {
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
sendHeartbeat: make(chan struct{}),
|
||||
processedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
@@ -431,7 +432,7 @@ func TestWorkerSession(t *testing.T) {
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
sendHeartbeat: make(chan struct{}),
|
||||
processedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
@@ -638,3 +639,110 @@ func TestWorkerSession(t *testing.T) {
|
||||
}, "expected wg to drain after parent cancel")
|
||||
})
|
||||
}
|
||||
|
||||
func TestWorkerSend(t *testing.T) {
|
||||
t.Run("data sent to mock socket", func(t *testing.T) {
|
||||
conn, _, _, outgoingData := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
heartbeat := make(chan struct{})
|
||||
heartbeatCount := atomic.Int32{}
|
||||
|
||||
w := &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
sendHeartbeat: heartbeat,
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
}
|
||||
w.conn.Store(conn)
|
||||
defer w.cancel()
|
||||
|
||||
go func() {
|
||||
for range heartbeat {
|
||||
heartbeatCount.Add(1)
|
||||
}
|
||||
}()
|
||||
|
||||
testData := []byte("hello")
|
||||
err := w.Send(testData)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// at least one heartbeat was sent
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
return heartbeatCount.Load() >= 1
|
||||
}, "expected heartbeats")
|
||||
|
||||
// message was sent by the socket
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
select {
|
||||
case msg := <-outgoingData:
|
||||
return string(msg.Data) == "hello"
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}, "expected message")
|
||||
})
|
||||
|
||||
t.Run("sends one heartbeat per successful send", func(t *testing.T) {
|
||||
conn, _, _, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
heartbeat := make(chan struct{})
|
||||
heartbeatCount := atomic.Int32{}
|
||||
|
||||
w := &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
sendHeartbeat: heartbeat,
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
}
|
||||
w.conn.Store(conn)
|
||||
defer w.cancel()
|
||||
|
||||
go func() {
|
||||
for range heartbeat {
|
||||
heartbeatCount.Add(1)
|
||||
}
|
||||
}()
|
||||
|
||||
const count = 3
|
||||
for i := range count {
|
||||
err := w.Send(fmt.Appendf(nil, "msg-%d", i))
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
return heartbeatCount.Load() == count
|
||||
}, "expected heartbeats")
|
||||
})
|
||||
|
||||
t.Run("returns error if connection is unavailable", func(t *testing.T) {
|
||||
// no connection available to worker
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
heartbeat := make(chan struct{})
|
||||
|
||||
w := &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
sendHeartbeat: heartbeat,
|
||||
}
|
||||
defer w.cancel()
|
||||
|
||||
go func() {
|
||||
for range heartbeat {
|
||||
}
|
||||
}()
|
||||
|
||||
err := w.Send([]byte("hello"))
|
||||
assert.ErrorIs(t, err, ErrConnectionUnavailable)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user