github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/cache_test.go (about) 1 package rpc 2 3 import ( 4 "cgl.tideland.biz/asserts" 5 "github.com/mitchellh/packer/packer" 6 "net/rpc" 7 "testing" 8 ) 9 10 type testCache struct { 11 lockCalled bool 12 lockKey string 13 unlockCalled bool 14 unlockKey string 15 rlockCalled bool 16 rlockKey string 17 runlockCalled bool 18 runlockKey string 19 } 20 21 func (t *testCache) Lock(key string) string { 22 t.lockCalled = true 23 t.lockKey = key 24 return "foo" 25 } 26 27 func (t *testCache) RLock(key string) (string, bool) { 28 t.rlockCalled = true 29 t.rlockKey = key 30 return "foo", true 31 } 32 33 func (t *testCache) Unlock(key string) { 34 t.unlockCalled = true 35 t.unlockKey = key 36 } 37 38 func (t *testCache) RUnlock(key string) { 39 t.runlockCalled = true 40 t.runlockKey = key 41 } 42 43 func TestCache_Implements(t *testing.T) { 44 var raw interface{} 45 raw = Cache(nil) 46 if _, ok := raw.(packer.Cache); !ok { 47 t.Fatal("Cache must be a cache.") 48 } 49 } 50 51 func TestCacheRPC(t *testing.T) { 52 assert := asserts.NewTestingAsserts(t, true) 53 54 // Create the interface to test 55 c := new(testCache) 56 57 // Start the server 58 server := rpc.NewServer() 59 RegisterCache(server, c) 60 address := serveSingleConn(server) 61 62 // Create the client over RPC and run some methods to verify it works 63 rpcClient, err := rpc.Dial("tcp", address) 64 assert.Nil(err, "should be able to connect") 65 client := Cache(rpcClient) 66 67 // Test Lock 68 client.Lock("foo") 69 assert.True(c.lockCalled, "should be called") 70 assert.Equal(c.lockKey, "foo", "should have proper key") 71 72 // Test Unlock 73 client.Unlock("foo") 74 assert.True(c.unlockCalled, "should be called") 75 assert.Equal(c.unlockKey, "foo", "should have proper key") 76 77 // Test RLock 78 client.RLock("foo") 79 assert.True(c.rlockCalled, "should be called") 80 assert.Equal(c.rlockKey, "foo", "should have proper key") 81 82 // Test RUnlock 83 client.RUnlock("foo") 84 assert.True(c.runlockCalled, "should be called") 85 assert.Equal(c.runlockKey, "foo", "should have proper key") 86 }