56 lines
1.3 KiB
Go
56 lines
1.3 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)
|
|
}
|
|
}
|