93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
package session
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/transport/tcp/protocol"
|
|
)
|
|
|
|
func TestOutboundDropDoesNotClose(t *testing.T) {
|
|
c := NewConn("1", "127.0.0.1:1", 1)
|
|
if err := c.Send(&Packet{Command: protocol.CmdHeartbeat}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := c.Send(&Packet{Command: protocol.CmdHeartbeat})
|
|
if err != errs.ErrOutboundDropped {
|
|
t.Fatalf("want drop, got %v", err)
|
|
}
|
|
if c.Drops() != 1 {
|
|
t.Fatalf("drops %d", c.Drops())
|
|
}
|
|
if c.State() == StateClosed {
|
|
t.Fatal("drop must not close")
|
|
}
|
|
}
|
|
|
|
func TestKickSameDevice(t *testing.T) {
|
|
m := NewManager()
|
|
var kicked []byte
|
|
old := NewConn("old", "10.0.0.1:1", 8)
|
|
old.Authenticate(7, 9)
|
|
old.SetIO(func(b []byte) error { kicked = append(kicked, b...); return nil }, nil)
|
|
if err := m.Bind(old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := NewConn("new", "10.0.0.1:2", 8)
|
|
n.Authenticate(7, 9)
|
|
if err := m.Bind(n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if old.State() != StateClosed {
|
|
t.Fatalf("old state %d", old.State())
|
|
}
|
|
got, err := protocol.Decode(kicked, 65536)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Command != protocol.CmdKick {
|
|
t.Fatalf("want kick, got %d", got.Command)
|
|
}
|
|
sessions := m.GetUserSessions(7)
|
|
if len(sessions) != 1 || sessions[0].ID() != "new" {
|
|
t.Fatalf("sessions %+v", sessions)
|
|
}
|
|
}
|
|
|
|
func TestKickDevice(t *testing.T) {
|
|
m := NewManager()
|
|
c := NewConn("s1", "10.0.0.1:1", 8)
|
|
c.Authenticate(3, 4)
|
|
if err := m.Bind(c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.KickDevice(3, 4)
|
|
if c.State() != StateClosed {
|
|
t.Fatalf("state %d", c.State())
|
|
}
|
|
if len(m.GetUserSessions(3)) != 0 {
|
|
t.Fatal("expected empty")
|
|
}
|
|
}
|
|
|
|
func TestKickUser(t *testing.T) {
|
|
m := NewManager()
|
|
a := NewConn("s1", "10.0.0.1:1", 8)
|
|
a.Authenticate(3, 4)
|
|
b := NewConn("s2", "10.0.0.1:2", 8)
|
|
b.Authenticate(3, 5)
|
|
if err := m.Bind(a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Bind(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.KickUser(3)
|
|
if a.State() != StateClosed || b.State() != StateClosed {
|
|
t.Fatalf("state %d %d", a.State(), b.State())
|
|
}
|
|
if len(m.GetUserSessions(3)) != 0 {
|
|
t.Fatal("expected empty")
|
|
}
|
|
}
|