-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomcache_test.go
107 lines (88 loc) · 2.76 KB
/
gomcache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright 2024 The gomcache AUTHORS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package gomcache provides a client for the Memcached cache server using TCP and UDP.
package gomcache
import (
"fmt"
"testing"
)
// MockServer simulates a Memcached server for testing purposes.
type MockServer struct {
Addr string
}
// NewMockServer creates a new mock server.
func NewMockServer(addr string) *MockServer {
return &MockServer{Addr: addr}
}
// Set simulates adding or updating an item in the mock server.
func (s *MockServer) Set(key string, value []byte) string {
return "STORED"
}
// Get simulates retrieving an item from the mock server.
func (s *MockServer) Get(key string) (string, []byte) {
if key == "existing_key" {
return "VALUE", []byte("test_value")
}
return "END", nil
}
// Delete simulates removing an item from the mock server.
func (s *MockServer) Delete(key string) string {
if key == "existing_key" {
return "DELETED"
}
return "NOT_FOUND"
}
// TestSet tests the Set method.
func TestSet(t *testing.T) {
mockServer := NewMockServer("localhost:11211")
client, _ := NewClient([]string{mockServer.Addr}, false)
item := &Item{
Key: "foo",
Value: []byte("barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),
}
err := client.Set(item)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}
// TestGet tests the Get method with UDP.
func TestGet(t *testing.T) {
mockServer := NewMockServer("localhost:11211")
client, _ := NewClient([]string{mockServer.Addr}, true)
// Simulate UDP response by using a real UDP connection or adapt the mockServer.
item, err := client.Get("foo")
fmt.Println("Item: ", item)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// if string(item.Value) != "test_value" {
// t.Fatalf("expected value %s, got %s", "test_value", string(item.Value))
// }
_, err = client.Get("non_existing_key")
if err == nil {
t.Fatalf("expected an error, got nil")
}
}
// TestDelete tests the Delete method.
func TestDelete(t *testing.T) {
mockServer := NewMockServer("localhost:11211")
client, _ := NewClient([]string{mockServer.Addr}, false)
err := client.Delete("foo")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
err = client.Delete("non_existing_key")
if err == nil {
t.Fatalf("expected an error, got nil")
}
}