Esempio 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.")
	}
}
Esempio n. 2
0
// check token by client public key
func ProcAuth(handler *coreprocessing.Handler, inIns *coreprocessing.CoreInstruction) *coreprocessing.CoreInstruction {
	// client sends in params:
	//  - json: {"key": "<key name>"}
	//  - data: "<base64 token string, use JWT protocol (random data inside)>"
	// getting (search on option.KeyDir) client public key by name (in command params)
	handler.Stat.AddOneMsg("auth_request")
	changes := connectionsupport.StateChanges{
		ChangeType: connectionsupport.StateChangesTypeAuth}
	var result *coreprocessing.CoreInstruction
	var resultErr error
	var errCode int
	if cmd, exists := inIns.GetCommand(); exists {
		if authData, err := newAuthData(cmd.Params); err == nil {
			if err := authData.Check(handler.Option); err != nil {
				errCode = transport.ErrorCodeMethodAuthFailed
				resultErr = err
			}
		} else {
			resultErr = err
			errCode = transport.ErrorCodeMethodParamsFormatWrong
		}
	} else {
		errCode = transport.ErrorCodeCommandFormatWrong
		resultErr = errors.New("Not found command in instruction.")
	}
	var answer *transport.Answer
	var insType int
	if errCode > 0 {
		changes.Auth = false
		answer = inIns.MakeErrAnswer(errCode, fmt.Sprint(resultErr))
		insType = coreprocessing.TypeInstructionProblem
		rllogger.Outputf(rllogger.LogWarn, "Failed auth from %s with error: %s", inIns.Cid, resultErr)
	} else {
		changes.Auth = true
		handler.Stat.AddOneMsg("auth_successfull")
		answer = inIns.MakeOkAnswer("{\"auth\":true}")
		insType = coreprocessing.TypeInstructionOk
		rllogger.Outputf(rllogger.LogDebug, "Successfull auth from %s", inIns.Cid)
	}
	result = coreprocessing.NewCoreInstruction(insType)
	result.SetAnswer(answer)
	result.StateChanges = &changes
	return result
}
Esempio n. 3
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")
	}
}