package storage import ( "encoding/json" "github.com/spf13/viper" "testing" "time" ) func TestEtcdGroupCacheHandler(t *testing.T) { cfg := createConfig() handler, err := NewEtcdGroupCacheHandler(cfg) if err != nil { t.Fatalf("failed to create EtcdGroupCacheHandler: %v", err) } key := "test-key" value := map[string]string{"key1": "value1", "key2": "value2"} // test Put and Get err = handler.Put(key, value) if err != nil { t.Fatalf("failed to put key: %v", err) } resp, err := handler.Get(key) if err != nil { t.Fatalf("failed to get key: %v", err) } var result map[string]string err = json.Unmarshal(resp.([]byte), &result) if err != nil { t.Fatalf("failed to unmarshal response: %v", err) } if len(result) != len(value) || result["key1"] != value["key1"] || result["key2"] != value["key2"] { t.Fatalf("unexpected response: %v", result) } // test PutWithTTL and Get err = handler.PutWithTTL(key, value, time.Second*2) if err != nil { t.Fatalf("failed to put key with TTL: %v", err) } resp, err = handler.Get(key) if err != nil { t.Fatalf("failed to get key: %v", err) } err = json.Unmarshal(resp.([]byte), &result) if err != nil { t.Fatalf("failed to unmarshal response: %v", err) } if len(result) != len(value) || result["key1"] != value["key1"] || result["key2"] != value["key2"] { t.Fatalf("unexpected response: %v", result) } // test Delete err = handler.Delete(key) if err != nil { t.Fatalf("failed to delete key: %v", err) } resp, err = handler.Get(key) if err == nil { t.Fatalf("expected key to be deleted") } } func createConfig() *viper.Viper { cfg := viper.New() cfg.Set("storage.kv.etcd.endpoints", []string{"localhost:2379"}) cfg.Set("storage.kv.etcd.username", "") cfg.Set("storage.kv.etcd.prefix", "parcoursmob/cache/") cfg.Set("storage.kv.etcd.password", "") cfg.Set("storage.kv.groupcache.endpoint", "localhost:8080") return cfg }