Ejemplo n.º 1
0
func TestConnectionsItems(t *testing.T) {
	stats := New(42, "9999", "8888", 1024, 2, true, true)
	storage := cache.New(42)
	if len(stats.Items(storage)) != 6 {
		t.Fatalf("Unexpected length of returned value; expected 6.")
	}
}
Ejemplo n.º 2
0
func TestSerializationCase1(t *testing.T) {
	stats := New(42, "9999", "8888", 1024, 2, true, true)
	storage := cache.New(42)
	if len(stats.Serialize(storage)) != 19 {
		t.Fatalf("Unexpected number of fields of returned value: ", stats)
	}
}
Ejemplo n.º 3
0
func TestSettingsSerialization(t *testing.T) {
	stats := New(42, "9999", "8888", 1024, 2, true, true)
	storage := cache.New(42)
	res := stats.Settings(storage)
	if len(res) != 12 {
		t.Fatalf("Unexpected length of Settings serialization: %d, expected 12;", len(res), res)
	}
}
Ejemplo n.º 4
0
func TestHandlingSuiteSet2(t *testing.T) {
	var storage = cache.New(4)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 42, 0, false, make([]byte, 42), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err == nil || string(res) != strings.Replace(SERVER_ERROR_TEMP, "%s", "Not enough memory", 1) {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 5
0
func TestHandlingSuiteFlushAll(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"flush_all", nil, 0, 0, 0, 0, false, nil, ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "OK\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 6
0
func TestHandlingSuiteSet1(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "STORED\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 7
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)
	}
}
Ejemplo n.º 8
0
func TestHandlingSuiteDelete2(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	testEnum.HandleRequest(storage, nil)
	testEnum = Ascii_protocol_enum{"delete", []string{"not_key"}, 0, 0, 0, 0, false, nil, ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != NOT_FOUND {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 9
0
func TestHandlingSuiteCas2(t *testing.T) {
	var storage = cache.New(42)
	if !storage.Set(tools.NewStoredData([]byte("test1"), "key"), 0, 0, 0) {
		t.Fatalf("Unexpecting behavior ")
	}
	var testEnum = Ascii_protocol_enum{"cas", []string{"key"}, 1, 0, 42, 424242, false, make([]byte, 42), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != NOT_FOUND {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 10
0
func TestHandlingSuiteGetMultiple(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key1"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	testEnum.HandleRequest(storage, nil)
	testEnum = Ascii_protocol_enum{"set", []string{"key2"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	testEnum.HandleRequest(storage, nil)
	testEnum = Ascii_protocol_enum{"get", []string{"key1", "key2"}, 0, 0, 0, 0, false, nil, ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "VALUE key1 1 4\r\nTEST\r\nVALUE key2 1 4\r\nTEST\r\nEND\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
}
Ejemplo n.º 11
0
func TestHandlingSuiteIncrDecr2(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 6, 0, false, []byte("3.1459"), ""}
	testEnum.HandleRequest(storage, nil)
	testEnum = Ascii_protocol_enum{"incr", []string{"key"}, 0, 0, 0, 0, false, []byte("100"), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "ERROR\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, string(res))
	}
	testEnum = Ascii_protocol_enum{"decr", []string{"key1"}, 0, 0, 0, 0, false, []byte("100"), ""}
	res, err = testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "NOT_FOUND\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, string(res))
	}
}
Ejemplo n.º 12
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
}
Ejemplo n.º 13
0
func TestHandlingSuitePrepend2(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "STORED\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	testEnum = Ascii_protocol_enum{"prepend", []string{"key"}, 0, 0, 5, 0, false, []byte("TEST2"), ""}
	res, err = testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "STORED\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	stored := tools.ExtractStoredData(storage.Get("key").Cacheable)
	if stored == nil || string(stored) != "TEST2TEST" {
		t.Fatalf("Stored value is invalid: ", err, stored)
	}
}
Ejemplo n.º 14
0
func TestHandlingSuiteGets1(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	testEnum = Ascii_protocol_enum{"gets", []string{"key"}, 0, 0, 0, 0, false, nil, ""}
	res, err = testEnum.HandleRequest(storage, nil)
	item := storage.Get("key")
	if item == nil {
		t.Fatalf("Item wasn't stored")
	}
	cas := item.Cas_unique
	if cas == 0 {
		t.Fatalf("Cas unique wasn't set.")
	}
	match_str := "VALUE key 1 4 " + tools.IntToString(cas) + "\r\nTEST\r\nEND\r\n"
	if err != nil || string(res) != match_str {
		t.Fatalf("Unexpected returned values of handling: ", err, res, testEnum)
	}
}
Ejemplo n.º 15
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)
	}
}
Ejemplo n.º 16
0
func TestSerializationCase2(t *testing.T) {
	stats := New(42, "9999", "8888", 1024, 2, true, true)
	storage := cache.New(42)
	stats.Commands["cmd_get"]++
	stats.Commands["cmd_set"]++
	stats.Commands["cmd_delete"]++
	stats.Commands["cmd_touch"]++
	stats.Commands["get_misses"]++
	stats.Commands["get_hits"]++
	stats.Commands["cas_misses"]++
	stats.Commands["cas_hits"]++
	stats.Commands["incr_misses"]++
	stats.Commands["incr_hits"]++
	stats.Commands["decr_misses"]++
	stats.Commands["decr_hits"]++
	stats.Commands["delete_misses"]++
	stats.Commands["delete_hits"]++
	stats.Commands["touch_misses"]++
	stats.Commands["touch_hits"]++
	stats.Commands["cas_badval"]++
	if len(stats.Serialize(storage)) != 36 {
		t.Fatalf("Unexpected number of fields of returned value: ", stats)
	}
}