You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB
Go

package server
import (
"testing"
nnetproto "github.com/noahlann/nnet/internal/protocol/nnet"
"github.com/stretchr/testify/assert"
)
func TestUnpackerManager_GetOrCreate_CachePerConn(t *testing.T) {
m := newUnpackerManager()
proto := nnetproto.NewNNetProtocol("1.0")
u1 := m.getOrCreateUnpacker("c1", proto)
assert.NotNil(t, u1)
// Same connection should return same instance
u2 := m.getOrCreateUnpacker("c1", proto)
assert.Equal(t, u1, u2)
// Different connection should get another instance
u3 := m.getOrCreateUnpacker("c2", proto)
assert.NotNil(t, u3)
// NNet protocol returns a singleton unpacker instance; expect same pointer
assert.Equal(t, u1, u3)
}
func TestUnpackerManager_GetOrCreate_NoProtocol(t *testing.T) {
m := newUnpackerManager()
u := m.getOrCreateUnpacker("c1", nil)
assert.Nil(t, u)
}
func TestUnpackerManager_Remove(t *testing.T) {
m := newUnpackerManager()
proto := nnetproto.NewNNetProtocol("1.0")
u1 := m.getOrCreateUnpacker("c1", proto)
assert.NotNil(t, u1)
m.removeUnpacker("c1")
// After removal, a new instance should be created
u2 := m.getOrCreateUnpacker("c1", proto)
assert.NotNil(t, u2)
// NNet protocol returns a singleton unpacker instance; expect same pointer again
assert.Equal(t, u1, u2)
}