Feature(Go): Screen frame relay end-to-end with graceful client BYE (Phase 4)

This commit is contained in:
yuanyuanxiang
2026-05-18 01:00:56 +02:00
parent b1f229706c
commit f013512c06
10 changed files with 999 additions and 74 deletions

View File

@@ -5,16 +5,22 @@ import (
"sync"
"testing"
"time"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/connection"
)
// stubCtx returns a non-nil *connection.Context useful only as a sentinel.
// Tests never invoke Send / Close on it.
func stubCtx() *connection.Context { return &connection.Context{} }
func TestHubRegisterListUnregister(t *testing.T) {
h := New()
if got := h.Count(); got != 0 {
t.Fatalf("empty hub: want Count=0, got %d", got)
}
h.Register(&Device{ID: "a", Name: "Alice", ConnectedAt: time.Now()})
h.Register(&Device{ID: "b", Name: "Bob", ConnectedAt: time.Now()})
h.Register(&Device{ID: "a", Name: "Alice", ConnectedAt: time.Now()}, stubCtx())
h.Register(&Device{ID: "b", Name: "Bob", ConnectedAt: time.Now()}, stubCtx())
if got := h.Count(); got != 2 {
t.Fatalf("after 2 registers: want Count=2, got %d", got)
}
@@ -38,8 +44,9 @@ func TestHubRegisterListUnregister(t *testing.T) {
func TestHubNilAndEmptyIgnored(t *testing.T) {
h := New()
h.Register(nil)
h.Register(&Device{ID: ""})
h.Register(nil, stubCtx())
h.Register(&Device{ID: ""}, stubCtx())
h.Register(&Device{ID: "valid"}, nil) // nil conn should also be rejected
h.Unregister("")
if got := h.Count(); got != 0 {
t.Fatalf("nil/empty register should be no-op, got Count=%d", got)
@@ -71,13 +78,19 @@ func (c *captureHandler) OnDeviceUpdate(id string, rtt int, _ string) {
c.mu.Unlock()
}
func (c *captureHandler) OnScreenFrame(_ string, _ []byte, _ bool) {}
func (c *captureHandler) OnResolutionChange(_ string, _, _ int) {}
func (c *captureHandler) OnCursorChange(_ string, _ byte) {}
func TestHubSubscribeEvents(t *testing.T) {
h := New()
c := &captureHandler{}
unsub := h.Subscribe(c)
h.Register(&Device{ID: "x", Name: "x"})
h.Register(&Device{ID: "y", Name: "y"})
h.Register(&Device{ID: "x", Name: "x"}, stubCtx())
h.Register(&Device{ID: "y", Name: "y"}, stubCtx())
h.Unregister("x")
h.Unregister("nonexistent") // no event
@@ -89,7 +102,7 @@ func TestHubSubscribeEvents(t *testing.T) {
}
unsub()
h.Register(&Device{ID: "z"})
h.Register(&Device{ID: "z"}, stubCtx())
if len(c.online) != 2 {
t.Fatalf("after unsubscribe should not receive events: %+v", c.online)
}
@@ -100,7 +113,7 @@ func TestHubUpdateLive(t *testing.T) {
c := &captureHandler{}
h.Subscribe(c)
h.Register(&Device{ID: "x", Name: "x"})
h.Register(&Device{ID: "x", Name: "x"}, stubCtx())
h.UpdateLive("x", 42, "Notepad")
h.UpdateLive("ghost", 999, "should be ignored") // unknown id, no event
@@ -116,8 +129,8 @@ func TestHubUpdateLive(t *testing.T) {
func TestHubRegisterOverwrites(t *testing.T) {
h := New()
h.Register(&Device{ID: "x", Name: "first"})
h.Register(&Device{ID: "x", Name: "second"})
h.Register(&Device{ID: "x", Name: "first"}, stubCtx())
h.Register(&Device{ID: "x", Name: "second"}, stubCtx())
list := h.ListDevices()
if len(list) != 1 || list[0].Name != "second" {
t.Fatalf("re-register should overwrite, got %+v", list)
@@ -137,7 +150,7 @@ func TestHubConcurrent(t *testing.T) {
defer wg.Done()
for i := range opsPer {
id := fmt.Sprintf("g%d-%d", g, i)
h.Register(&Device{ID: id, Name: id, ConnectedAt: time.Now()})
h.Register(&Device{ID: id, Name: id, ConnectedAt: time.Now()}, stubCtx())
_ = h.ListDevices()
_ = h.Count()
h.Unregister(id)