2013-06-09 22:25:48 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testCache struct {
|
|
|
|
lockCalled bool
|
|
|
|
lockKey string
|
|
|
|
unlockCalled bool
|
|
|
|
unlockKey string
|
|
|
|
rlockCalled bool
|
|
|
|
rlockKey string
|
|
|
|
runlockCalled bool
|
|
|
|
runlockKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCache) Lock(key string) string {
|
|
|
|
t.lockCalled = true
|
|
|
|
t.lockKey = key
|
|
|
|
return "foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCache) RLock(key string) (string, bool) {
|
|
|
|
t.rlockCalled = true
|
|
|
|
t.rlockKey = key
|
|
|
|
return "foo", true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCache) Unlock(key string) {
|
|
|
|
t.unlockCalled = true
|
|
|
|
t.unlockKey = key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCache) RUnlock(key string) {
|
|
|
|
t.runlockCalled = true
|
|
|
|
t.runlockKey = key
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_Implements(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = Cache(nil)
|
|
|
|
if _, ok := raw.(packer.Cache); !ok {
|
|
|
|
t.Fatal("Cache must be a cache.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheRPC(t *testing.T) {
|
|
|
|
// Create the interface to test
|
|
|
|
c := new(testCache)
|
|
|
|
|
|
|
|
// Start the server
|
2013-12-09 19:22:11 -05:00
|
|
|
client, server := testClientServer(t)
|
2013-12-09 17:51:13 -05:00
|
|
|
defer client.Close()
|
2013-12-09 19:22:11 -05:00
|
|
|
defer server.Close()
|
|
|
|
server.RegisterCache(c)
|
|
|
|
|
2013-12-09 17:51:13 -05:00
|
|
|
cacheClient := client.Cache()
|
2013-06-09 22:25:48 -04:00
|
|
|
|
|
|
|
// Test Lock
|
2013-12-09 17:51:13 -05:00
|
|
|
cacheClient.Lock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !c.lockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if c.lockKey != "foo" {
|
|
|
|
t.Fatalf("bad: %s", c.lockKey)
|
|
|
|
}
|
2013-06-09 22:25:48 -04:00
|
|
|
|
|
|
|
// Test Unlock
|
2013-12-09 17:51:13 -05:00
|
|
|
cacheClient.Unlock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !c.unlockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if c.unlockKey != "foo" {
|
|
|
|
t.Fatalf("bad: %s", c.unlockKey)
|
|
|
|
}
|
2013-06-09 22:25:48 -04:00
|
|
|
|
|
|
|
// Test RLock
|
2013-12-09 17:51:13 -05:00
|
|
|
cacheClient.RLock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !c.rlockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if c.rlockKey != "foo" {
|
|
|
|
t.Fatalf("bad: %s", c.rlockKey)
|
|
|
|
}
|
2013-06-09 22:25:48 -04:00
|
|
|
|
|
|
|
// Test RUnlock
|
2013-12-09 17:51:13 -05:00
|
|
|
cacheClient.RUnlock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !c.runlockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if c.runlockKey != "foo" {
|
|
|
|
t.Fatalf("bad: %s", c.runlockKey)
|
|
|
|
}
|
2013-06-09 22:25:48 -04:00
|
|
|
}
|