Example #1
0
func TestStatisticSimple(t *testing.T) {
	option := options.SysOption{
		Statistic: true, StatisticCheckTime: 2}
	stat := statistic.NewStatistic(option)
	outHandler := func(now time.Time, lines *[]string) {
		for _, line := range *lines {
			t.Log(line)
		}
	}
	calcSummMethod := func(items *map[string]statistic.StatValueType) {
		var sum statistic.StatValueType
		if val, exists := (*items)["test1"]; exists {
			sum += val
		}
		if val, exists := (*items)["test2"]; exists {
			sum += val
		}
		(*items)["test3"] = sum
	}
	rllogger.SetSilent(true)
	stat.AddStatisticOutHandler(outHandler)
	stat.AddCalcHandler(calcSummMethod)
	stat.AddItem("test1", "Test value 1")
	stat.AddItem("test2", "Test value 2")
	stat.AddItem("test3", "Test value 3")
	stat.SendMsg("test2", 10)
	stat.SendMsg("test1", 5.5)
	time.Sleep(time.Duration(option.StatisticCheckTime+1) * time.Second)
	stat.Close()
	testData := statistic.TestingData(stat)
	items := testData.GetTestingData()
	if val, exists := (*items)["test3"]; !exists || val != 15.5 {
		t.Error("Calculation problem!")
	}
}
func TestUpdateStatusChanged(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	var newStatus uint16 = 1
	stat := statistic.NewStatistic(option)
	handler := coreprocessing.NewHandler(1, option, stat)
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, "27d90e5e-0000000000000011-1", "statusupdate", fmt.Sprint(newStatus))
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcUpdateStatus(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		if (*answer).Error.Code > 0 {
			t.Errorf(
				"Answer has error with code: %d msg: %s.",
				(*answer).Error.Code,
				(*answer).Error.Message)
		} else {
			if outIns.StateChanges != nil {
				if outIns.StateChanges.Status != newStatus {
					t.Errorf("State change delegator has wrong status: %d.", outIns.StateChanges.Status)
				}
			} else {
				t.Error("Empty state change delegator.")
			}
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}
// ProcRegistration=>
//
func TestRegistrationEmptyParams(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	stat := statistic.NewStatistic(option)
	handler := coreprocessing.NewHandler(1, option, stat)
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, "27d90e5e-0000000000000011-1", "registration", "")
	// expected methods as list of string
	(*cmd).Params.Json = "{\"methods\": \"none\", \"group\": 1}"
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcRegistration(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		err := (*answer).Error.Code
		if err > 0 {
			t.Logf("Answer has error, %s.", (*answer).Error)
			if err != transport.ErrorCodeMethodParamsFormatWrong {
				t.Error("Unexpected error code: %d.", err)
			}
		} else {
			t.Error("This answer must have error.")
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}
func TestRegistrationUnknownGroup(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	stat := statistic.NewStatistic(option)
	handler := coreprocessing.NewHandler(1, option, stat)
	cheker := forTestConnectionStateCheck{Auth: true}
	handler.StateCheker = &cheker
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, "27d90e5e-0000000000000011-1", "registration", "")
	(*cmd).Params.Json = "{\"methods\": [\"none\"], \"group\": 4}"
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcRegistration(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		err := (*answer).Error.Code
		if err > 0 {
			t.Logf("Answer has error, %s.", (*answer).Error)
			if err != transport.ErrorCodeUnexpectedValue {
				t.Error("Unexpected error code: %d.", err)
			}
		} else {
			t.Error("This answer must have error.")
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}
Example #5
0
func Launch(option *options.SysOption) {
	signalChannel := make(chan os.Signal, 1)
	signal.Notify(signalChannel, os.Interrupt)
	signal.Notify(signalChannel, syscall.SIGTERM)
	coremethods.Setup()
	mustExit := false
	stat := statistic.NewStatistic(*option)
	manager := coresupport.NewCoreWorkerManager(*option, stat)
	server := connectionserver.NewServer(*option, stat)
	server.Start(manager)
	manager.Start(server)
	// wait
	for !mustExit {
		select {
		case newSig := <-signalChannel:
			{
				if newSig != nil {
					rllogger.Output(rllogger.LogInfo, "Stoping service, now wait..")
					server.Stop()
					manager.Stop()
				}
			}
		case <-manager.OutSignalChannel:
			{
				rllogger.Output(rllogger.LogInfo, "Close manager.")
				mustExit = true
			}
		}
	}
	manager.Close()
	stat.Close()
	close(signalChannel)
}
// ProcUpdateStatus =>
//
func TestUpdateStatusEmptyParams(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	stat := statistic.NewStatistic(option)
	handler := coreprocessing.NewHandler(1, option, stat)
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, "27d90e5e-0000000000000011-1", "statusupdate", "")
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcUpdateStatus(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		if (*answer).Error.Code != transport.ErrorCodeMethodParamsFormatWrong {
			t.Errorf("Answer hasn't error with code: %d.", transport.ErrorCodeMethodParamsFormatWrong)
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}
func TestRegistratioAddGroupClient(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	stat := statistic.NewStatistic(option)
	cid := "27d90e5e-0000000000000012-1"
	handler := coreprocessing.NewHandler(1, option, stat)
	cheker := forTestConnectionStateCheck{Auth: true}
	handler.StateCheker = &cheker
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, cid, "registration", "")
	(*cmd).Params.Json = fmt.Sprintf(
		"{\"group\": %d}", connectionsupport.GroupConnectionClient)
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcRegistration(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		err := (*answer).Error.Code
		if err > 0 {
			t.Error("Answer with problem, %s", (*answer).Error)
		} else {
			stCh := (*outIns).StateChanges
			if stCh != nil {
				if (*stCh).ChangeType == connectionsupport.StateChangesTypeGroup {
					if (*stCh).ConnectionClientGroup == connectionsupport.GroupConnectionClient {
						if answer, exists := outIns.GetAnswer(); exists {
							if (*answer).Result != "{\"ok\": true}" {
								t.Errorf("Incorrect answer data: %s", (*answer).Result)
							}
						} else {
							t.Error("Answer data is empty.")
						}
					} else {
						t.Errorf("Incorrect connection group %s in changes.", (*stCh).ConnectionClientGroup)
					}
				} else {
					t.Errorf("Type of changes is not a {}", connectionsupport.StateChangesTypeGroup)
				}

			} else {
				t.Error("Changes empty.")
			}
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}
Example #8
0
func TestStatisticFileSave(t *testing.T) {
	option := options.SysOption{
		Statistic:          true,
		StatisticFile:      fmt.Sprintf("%s/roolettest.stat.log", os.TempDir()),
		StatisticCheckTime: 1}

	t.Logf("Statistic file: %s", option.StatisticFile)
	stat := statistic.NewStatistic(option)
	calcSummMethod := func(items *map[string]statistic.StatValueType) {
		var sum statistic.StatValueType
		if val, exists := (*items)["test1"]; exists {
			sum += val
		}
		if val, exists := (*items)["test2"]; exists {
			sum += val
		}
		(*items)["test3"] = sum
	}
	rllogger.SetSilent(true)
	stat.AddCalcHandler(calcSummMethod)
	stat.AddItem("test1", "Test value 1")
	stat.AddItem("test2", "Test value 2")
	stat.AddItem("test3", "Test value 3")
	stat.SendMsg("test2", 10)
	stat.SendMsg("test1", 5.5)
	time.Sleep(time.Duration(option.StatisticCheckTime+1) * time.Second)
	stat.Close()
	if file, err := os.Open(option.StatisticFile); err != nil {
		t.Error(err)
	} else {
		if fi, err := file.Stat(); err != nil {
			t.Error(err)
		} else {
			fSize := fi.Size()
			t.Logf("File size: %d", fSize)
			if fSize != 81 {
				t.Error("Statistic file format changed??")
			}
		}
		file.Close()
		os.Remove(option.StatisticFile)
	}
}
Example #9
0
func TestUnexpectedClosingForAsyncClients(t *testing.T) {
	option := options.SysOption{
		Statistic: true, StatisticCheckTime: 2}
	stat := statistic.NewStatistic(option)
	outHandler := func(now time.Time, lines *[]string) {
		for _, line := range *lines {
			t.Log(line)
		}
	}
	calcSummMethod := func(items *map[string]statistic.StatValueType) {
		var sum statistic.StatValueType
		if val, exists := (*items)["test1"]; exists {
			sum += val
		}
		if val, exists := (*items)["test2"]; exists {
			sum += val
		}
		(*items)["summ"] = sum
	}
	calcAvgMethod := func(items *map[string]statistic.StatValueType) {
		var sum statistic.StatValueType
		if val, exists := (*items)["test1"]; exists {
			sum += val
		}
		if val, exists := (*items)["test2"]; exists {
			sum += val
		}
		(*items)["avg"] = sum / 2.0
	}
	rllogger.SetSilent(true)
	stat.AddStatisticOutHandler(outHandler)
	stat.AddCalcHandler(calcSummMethod)
	stat.AddCalcHandler(calcAvgMethod)
	stat.AddItem("test1", "Test value 1")
	stat.AddItem("test2", "Test value 2")
	stat.AddItem("count", "count")
	stat.AddItem("avg", "avg")
	stat.AddItem("summ", "summ")

	worker := func(st *statistic.Statistic, n int) {
		for i := 0; i < n; i++ {
			stat.SendMsg("test2", 1)
			stat.SendMsg("test1", 0.1)
			stat.SendMsg("test2", 0.1)
			stat.SendMsg("test1", 1)
			stat.SendMsg("count", 1)
		}
	}
	for index := 0; index <= 64; index++ {
		go worker(stat, 4000)
	}
	time.Sleep(time.Duration(100) * time.Millisecond)
	stat.Close()
	testData := statistic.TestingData(stat)
	items := testData.GetTestingData()
	if val, exists := (*items)["count"]; exists && val > 10 {
		t.Logf("count = %d", int(val))
	} else {
		t.Error("some problem")
	}
}
func TestRegistratioAddGroupServer(t *testing.T) {
	option := options.SysOption{
		Statistic: false}
	stat := statistic.NewStatistic(option)
	methodName := "test_test"
	cid := "27d90e5e-0000000000000011-1"
	handler := coreprocessing.NewHandler(1, option, stat)
	cheker := forTestConnectionStateCheck{Auth: true}
	handler.StateCheker = &cheker
	inIns := coreprocessing.NewCoreInstruction(coreprocessing.TypeInstructionReg)
	cmd := transport.NewCommand(0, cid, "registration", "")
	(*cmd).Params.Json = fmt.Sprintf(
		"{\"methods\": [\"%s\"], \"group\": %d}", methodName, connectionsupport.GroupConnectionServer)
	inIns.SetCommand(cmd)
	outIns := coremethods.ProcRegistration(handler, inIns)
	if answer, exists := outIns.GetAnswer(); exists {
		err := (*answer).Error.Code
		if err > 0 {
			t.Error("Answer with problem, %s", (*answer).Error)
		} else {
			stCh := (*outIns).StateChanges
			if stCh != nil {
				if (*stCh).ChangeType == connectionsupport.StateChangesTypeGroup {
					if (*stCh).ConnectionClientGroup == connectionsupport.GroupConnectionServer {
						cidMethods := coreprocessing.NewRpcServerManager()
						cidList := cidMethods.GetCidVariants(methodName)
						if len(cidList) > 0 {
							exists := false
							for _, variant := range cidList {
								t.Logf("%s == %s ?", cid, variant)
								if variant == cid {
									exists = true
								}
							}
							if exists {
								// check answer
								if answer, exists := outIns.GetAnswer(); exists {
									if (*answer).Result != "{\"methods_count\": 1, \"ok\": true}" {
										t.Errorf("Incorrect answer data: %s", (*answer).Result)
									}
								} else {
									t.Error("Answer data is empty.")
								}
							} else {
								t.Errorf("Not found cid '%s' for method '%s'.", cid, methodName)
							}
						} else {
							t.Errorf("New cid '%s' lost for method '%s'.", cid, methodName)
						}
					} else {
						t.Errorf("Incorrect connection group %s in changes.", (*stCh).ConnectionClientGroup)
					}
				} else {
					t.Errorf("Type of changes is not a {}", connectionsupport.StateChangesTypeGroup)
				}

			} else {
				t.Error("Changes empty.")
			}
		}
	} else {
		t.Errorf("Empty answer to %s.", *cmd)
	}
}