Пример #1
0
func TestHandlingStatistic(t *testing.T) {
	var testEnum = Ascii_protocol_enum{"stats", nil, 0, 0, 0, 0, false, nil, ""}
	var stats = stat.New(42, "9999", "8888", 1024, 2, true, true)
	var storage = cache.New(42)

	res, err := testEnum.HandleRequest(storage, stats)
	if err != nil || res == nil {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Пример #2
0
func TestHandlingStatsRecording(t *testing.T) {
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 2, 0, true, []byte("42"), ""}
	stats := stat.New(42, "9999", "8888", 1024, 2, true, true)
	var storage = cache.New(42)
	res, err := testEnum.HandleRequest(storage, stats)
	if err != nil || res == nil {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	if stats.Commands == nil || stats.Commands["cmd_set"] != 1 {
		t.Fatalf("Wrong stats handling: ", stats.Commands)
	}

	stats = stat.New(42, "9999", "8888", 1024, 2, true, true)
	testEnum = Ascii_protocol_enum{"get", []string{"not_key"}, 0, 0, 0, 0, false, nil, ""}
	res, err = testEnum.HandleRequest(storage, stats)
	if err != nil || res == nil {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	if stats.Commands == nil || stats.Commands["cmd_get"] != 1 || stats.Commands["get_misses"] != 1 {
		t.Fatalf("Wrong stats handling: ", stats.Commands)
	}

	stats = stat.New(42, "9999", "8888", 1024, 2, true, true)
	testEnum = Ascii_protocol_enum{"get", []string{"key"}, 0, 0, 0, 0, false, nil, ""}
	res, err = testEnum.HandleRequest(storage, stats)
	if err != nil || res == nil {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	if stats.Commands == nil || stats.Commands["cmd_get"] != 1 || stats.Commands["get_hits"] != 1 {
		t.Fatalf("Wrong stats handling: ", stats.Commands)
	}

	stats = stat.New(42, "9999", "8888", 1024, 2, true, true)
	testEnum = Ascii_protocol_enum{"cas", []string{"key"}, 1, 0, 42, 424242, false, make([]byte, 42), ""}
	res, err = testEnum.HandleRequest(storage, stats)
	if err != nil || res == nil {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	if stats.Commands == nil || stats.Commands["cas_badval"] != 1 {
		t.Fatalf("Wrong stats handling: ", stats.Commands)
	}
}
Пример #3
0
// This public function raises up the server.
// Function receives following params:
// tcp_port string, which uses to open tcp socket at pointed port,
// udp_port string, which uses to open tcp socket at pointed port,
// address, which specified an only ip address which server will listen to,
// max_connections, sets a limit of maximal number of active connections,
// cas, flush - flags which forbids of usage such commands if value = true,
// verbosity - defines the dept of verbosity for server
// and bytes_of_memory, which uses for limiting allocated memory.
// Function returns a pointer to a server structure with filled and prepared to usage fields.
func NewServer(tcp_port string, udp_port string, address string, max_connections int, cas bool, flush bool,
	verbosity int, bytes_of_memory int64) *Server {
	server := new(Server)
	server.tcp_socket = nil
	server.tcp_port = tcp_port
	server.udp_port = udp_port
	server.cas_disabled = cas
	server.flush_disabled = flush
	server.connection_limit = max_connections
	server.listen_address = address
	server.storage = cache.New(bytes_of_memory)
	server.connections = make(map[string]net.Conn)
	server.Stat = statistic.New(bytes_of_memory, tcp_port, udp_port, max_connections, verbosity, cas, flush)
	server.Logger = NewServerLogger(verbosity)
	return server
}