Last active
August 29, 2015 14:15
-
-
Save tysonmote/09b91bb4354e7d7c2edf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The following test runs against a local Consul server started like so: | |
// | |
// consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul | |
// | |
// The test output looks like this: | |
// | |
// % go test -test.v | |
// === RUN TestConsul | |
// --- PASS: TestConsul (6.76s) | |
// consul_test.go:188: LastIndex after #1: 5 | |
// consul_test.go:189: Waited for: 452.639µs | |
// consul_test.go:205: LastIndex after #2: 5 | |
// consul_test.go:206: Waited for: 5.0008878s | |
// PASS | |
// | |
// I expect the second "List" call to return after about one second (when a key | |
// is deleted), not after 5 seconds (when WaitTime expires). The LastIndex | |
// value does not change after the key is deleted. | |
package test | |
import ( | |
"github.com/hashicorp/consul/api" | |
"testing" | |
"time" | |
) | |
func TestConsul(t *testing.T) { | |
client, _ := api.NewClient(api.DefaultConfig()) | |
kv := client.KV() | |
kv.Put(&api.KVPair{Key: "test/foo", Value: []byte{}}, nil) | |
kv.Put(&api.KVPair{Key: "test/bar", Value: []byte{}}, nil) | |
start := time.Now() | |
pairs, meta, _ := kv.List("test", nil) | |
t.Log("LastIndex after #1:", meta.LastIndex) | |
t.Log("Waited for:", time.Since(start)) | |
if len(pairs) != 2 { | |
t.Errorf("got %d keys", len(pairs)) | |
} | |
// Delete a key in one second | |
go func() { | |
time.Sleep(time.Second) | |
kv.Delete("test/foo", nil) | |
// Uncomment to cause last List() call to return after 1 second: | |
// kv.Put(&api.KVPair{Key: "test/bar", Value: []byte{}}, nil) | |
}() | |
// Wait for test/foo to be deleted | |
start = time.Now() | |
pairs, meta, _ = kv.List("test/", &api.QueryOptions{ | |
WaitIndex: meta.LastIndex, | |
WaitTime: 5 * time.Second, | |
}) | |
t.Log("LastIndex after #2:", meta.LastIndex) | |
t.Log("Waited for:", time.Since(start)) | |
if len(pairs) != 1 { | |
t.Errorf("ERROR: expected 1 key, got: %#v", len(pairs)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment