コード例 #1
0
ファイル: hub.go プロジェクト: sech90/go-message-hub
func NewHub(port int) *Hub {

	// listen on all interfaces
	ls, err := net.Listen("tcp", ":"+strconv.Itoa(port))

	log.Printf("Server begin listen at port: %d", port)

	//chec for connection error
	if err != nil {
		log.Fatalln(err)
		return nil
	}

	hub := &Hub{

		listener:  ls,
		quit:      make(chan bool),
		idPool:    idpool.NewReusableIdPool(),
		idSet:     set.New(),
		socketMap: syncmap.NewSyncMap(),
	}

	return hub

}
コード例 #2
0
ファイル: idpool_test.go プロジェクト: sech90/go-message-hub
func TestReusablePool(t *testing.T) {
	assert := assert.New(t)
	pool := idpool.NewReusableIdPool()

	assert.Equal(uint64(1), pool.GetId(), "Id should start by 1")
	assert.Equal(uint64(2), pool.GetId(), "IdPool should give incremental ids")
	assert.Equal(uint64(3), pool.GetId(), "IdPool should give incremental ids")
	assert.Equal(uint64(4), pool.GetId(), "IdPool should give incremental ids")

	pool.ReleaseId(2)
	assert.Equal(uint64(2), pool.GetId(), "IdPool should reuse id")
	assert.Equal(uint64(5), pool.GetId(), "IdPool should give incremental ids")

	pool.ReleaseId(1)
	pool.ReleaseId(4)
	pool.ReleaseId(2)
	pool.ReleaseId(5)

	assert.Equal(uint64(1), pool.GetId(), "IdPool should reuse ids in a FIFO")
	assert.Equal(uint64(4), pool.GetId(), "IdPool should reuse ids in a FIFO")
	assert.Equal(uint64(2), pool.GetId(), "IdPool should reuse ids in a FIFO")
	assert.Equal(uint64(5), pool.GetId(), "IdPool should reuse ids in a FIFO")
}
コード例 #3
0
ファイル: idpool_test.go プロジェクト: sech90/go-message-hub
package idpool_test

import (
	"github.com/sech90/go-message-hub/hub/idpool"
	"github.com/stretchr/testify/assert"
	"testing"
)

var p1 = idpool.NewIncrementalPool()
var p2 = idpool.NewReusableIdPool()

func BenchmarkIncrementalPool(b *testing.B) {
	for n := 0; n < b.N; n++ {
		i := p1.GetId()
		p1.ReleaseId(i)
	}
}

func BenchmarkReusablePool(b *testing.B) {
	for n := 0; n < b.N; n++ {
		i := p2.GetId()
		p2.ReleaseId(i)
	}
}

func TestIncrementalPool(t *testing.T) {

	assert := assert.New(t)
	pool := idpool.NewIncrementalPool()

	assert.Equal(uint64(1), pool.GetId(), "Id should start by 1")