feat: tune websocket hub backpressure
This commit is contained in:
42
internal/transport/httpserver/hub_options_test.go
Normal file
42
internal/transport/httpserver/hub_options_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.yuntuo.hk/k2safe/orbit/platform/events"
|
||||
"git.yuntuo.hk/k2safe/orbit/pulse/worker"
|
||||
)
|
||||
|
||||
func TestHubOptionsAreBoundedAndApplied(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
workerOptions := resolveHubOptions([]HubOptions{{ConnectionLimit: 7, DispatchWorkers: 3, DispatchCapacity: 11}})
|
||||
if workerOptions.ConnectionLimit != 7 || workerOptions.DispatchWorkers != 3 || workerOptions.DispatchCapacity != 11 {
|
||||
t.Fatalf("worker hub options not resolved: %+v", workerOptions)
|
||||
}
|
||||
workerHub := NewWorkerHub(ctx, worker.New(nil), workerOptions)
|
||||
if cap(workerHub.slots) != 7 {
|
||||
t.Fatalf("worker connection limit not applied: %d", cap(workerHub.slots))
|
||||
}
|
||||
eventOptions := resolveHubOptions([]HubOptions{{ConnectionLimit: 5, DispatchWorkers: 2, DispatchCapacity: 9}})
|
||||
eventHub := NewEventHub(ctx, events.New(nil), eventOptions)
|
||||
if cap(eventHub.slots) != 5 {
|
||||
t.Fatalf("event connection limit not applied: %d", cap(eventHub.slots))
|
||||
}
|
||||
workerHub.Shutdown(context.Background())
|
||||
eventHub.Shutdown(context.Background())
|
||||
}
|
||||
|
||||
func TestHubOptionsRejectInvalidValues(t *testing.T) {
|
||||
for _, options := range []HubOptions{{ConnectionLimit: -1}, {ConnectionLimit: maxHubConnectionLimit + 1}, {DispatchWorkers: -1}, {DispatchWorkers: maxHubDispatchWorkers + 1}, {DispatchCapacity: -1}, {DispatchCapacity: maxHubDispatchCapacity + 1}} {
|
||||
t.Run("invalid", func(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("invalid hub options did not panic")
|
||||
}
|
||||
}()
|
||||
resolveHubOptions([]HubOptions{options})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user