|
| 1 | +package router //nolint:testpackage |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "testing/synctest" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/gammazero/nexus/v3/wamp" |
| 11 | +) |
| 12 | + |
| 13 | +// TestKillSessionsByAuthidGoAPI covers the Go-level Router.KillSessionsByAuthid |
| 14 | +// added for embedders (the cluster mesh) to enforce single-authority |
| 15 | +// identities: it closes matching sessions except the excluded one, returns the |
| 16 | +// kill count, and reports -1 for an unknown realm. |
| 17 | +func TestKillSessionsByAuthidGoAPI(t *testing.T) { |
| 18 | + synctest.Test(t, func(t *testing.T) { |
| 19 | + r := newTestRouter(t) |
| 20 | + defer r.Close() |
| 21 | + |
| 22 | + // testClient gives every session authid "user1". |
| 23 | + keep := testClient(t, r) |
| 24 | + victim1 := testClient(t, r) |
| 25 | + victim2 := testClient(t, r) |
| 26 | + |
| 27 | + // Unknown realm → -1, nothing killed. |
| 28 | + require.Equal(t, -1, r.KillSessionsByAuthid("no.such.realm", "user1", 0)) |
| 29 | + |
| 30 | + // Kill all "user1" sessions except `keep`. |
| 31 | + killed := r.KillSessionsByAuthid(testRealm, "user1", keep.ID) |
| 32 | + require.Equal(t, 2, killed, "expected both non-excluded sessions killed") |
| 33 | + |
| 34 | + for i, v := range []*wamp.Session{victim1, victim2} { |
| 35 | + msg, err := wamp.RecvTimeout(v, time.Second) |
| 36 | + require.NoErrorf(t, err, "victim %d should receive GOODBYE", i+1) |
| 37 | + g, ok := msg.(*wamp.Goodbye) |
| 38 | + require.Truef(t, ok, "victim %d expected GOODBYE", i+1) |
| 39 | + require.Equal(t, wamp.CloseNormal, g.Reason) |
| 40 | + } |
| 41 | + |
| 42 | + // The excluded session is untouched. |
| 43 | + _, err := wamp.RecvTimeout(keep, time.Millisecond) |
| 44 | + require.Error(t, err, "excluded session must not be killed") |
| 45 | + |
| 46 | + // A non-matching authid kills nothing. |
| 47 | + require.Equal(t, 0, r.KillSessionsByAuthid(testRealm, "nobody", 0)) |
| 48 | + }) |
| 49 | +} |
0 commit comments