Ejemplo n.º 1
0
func TestSimpleClientStateSyncUpdate(t *testing.T) {
	option := options.SysOption{}
	manager := connectionsupport.NewConnectionDataManager(option)
	// skip someone
	for i := 0; i < 2356; i++ {
		manager.NewConnection()
	}
	changes := connectionsupport.StateChanges{ChangeType: connectionsupport.StateChangesTypeAll}
	connData := manager.NewConnection()
	t.Logf("New client: %s", connData.Cid)
	changes.Status = connectionsupport.ClientStatusBusy
	manager.UpdateState(connData.Cid, changes)
	if !manager.ClientBusy(connData.Cid) {
		t.Error("Broken method 1.")
	}
	changes.Status = connectionsupport.ClientStatusActive
	manager.UpdateState(connData.Cid, changes)
	if manager.ClientBusy(connData.Cid) {
		t.Error("Broken method 2.")
	}
	changes.Status = connectionsupport.ClientStatusBusy
	manager.UpdateState(connData.Cid, changes)
	if !manager.ClientBusy(connData.Cid) {
		t.Error("Broken method 3.")
	}
}
Ejemplo n.º 2
0
func TestNormalDistributionOfMissesAndHits(t *testing.T) {
	option := options.SysOption{}
	manager := connectionsupport.NewConnectionDataManager(option)
	wCount := 100
	operCount := 1000
	for i := 0; i < 3462; i++ {
		manager.NewConnection()
	}
	connData := manager.NewConnection()
	t.Logf("New client: %s", connData.Cid)
	type res struct {
		Hits   int
		Misses int
		Done   bool
	}
	changes := connectionsupport.StateChanges{ChangeType: connectionsupport.StateChangesTypeAll}

	worker := func(done *chan res, m *connectionsupport.ConnectionDataManager, cid string) {
		r := res{}
		for i := 0; i < operCount; i++ {
			isActive := rand.Intn(100) > 50
			if isActive {
				changes.Status = connectionsupport.ClientStatusActive
			} else {
				changes.Status = connectionsupport.ClientStatusBusy
			}

			if isActive == m.ClientBusy(connData.Cid) {
				r.Hits++
			} else {
				r.Misses++
			}
			m.UpdateState(cid, changes)
		}
		r.Done = true
		(*done) <- r
	}
	doneChan := make(chan res, 1)
	for index := 0; index < wCount; index++ {
		go worker(&doneChan, manager, connData.Cid)
	}
	doneCount := 0
	misses := 0
	hits := 0
	for doneCount < wCount {
		newR := <-doneChan
		if newR.Done {
			misses += newR.Misses
			hits += newR.Hits
			doneCount++
		}
	}
	t.Logf("hits: %d misses: %d", hits, misses)
	total := hits + misses
	if total != wCount*operCount {
		t.Error("Test broken!")
	}
	// count hits/misses must > 40% of total
	if float32(hits)/float32(total)*100.0 < 40.0 {
		t.Error("Async work problem with hits")
	}
	if float32(misses)/float32(total)*100.0 < 40.0 {
		t.Error("Async work problem with misses")
	}
}