cleanup and refactors

This commit is contained in:
Jay
2026-05-20 22:49:25 -04:00
parent cda6d286ab
commit f1afca7921
10 changed files with 628 additions and 496 deletions
+113 -5
View File
@@ -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)
})
}