diff --git a/packer/rpc/cache_test.go b/packer/rpc/cache_test.go index 46cb81d08..3fce1ee4a 100644 --- a/packer/rpc/cache_test.go +++ b/packer/rpc/cache_test.go @@ -2,7 +2,6 @@ package rpc import ( "github.com/mitchellh/packer/packer" - "net/rpc" "testing" ) @@ -52,19 +51,14 @@ func TestCacheRPC(t *testing.T) { c := new(testCache) // Start the server - server := rpc.NewServer() - RegisterCache(server, c) - address := serveSingleConn(server) - - // Create the client over RPC and run some methods to verify it works - rpcClient, err := rpc.Dial("tcp", address) - if err != nil { - t.Fatalf("bad: %s", err) - } - client := Cache(rpcClient) + server := NewServer() + server.RegisterCache(c) + client := testClient(t, server) + defer client.Close() + cacheClient := client.Cache() // Test Lock - client.Lock("foo") + cacheClient.Lock("foo") if !c.lockCalled { t.Fatal("should be called") } @@ -73,7 +67,7 @@ func TestCacheRPC(t *testing.T) { } // Test Unlock - client.Unlock("foo") + cacheClient.Unlock("foo") if !c.unlockCalled { t.Fatal("should be called") } @@ -82,7 +76,7 @@ func TestCacheRPC(t *testing.T) { } // Test RLock - client.RLock("foo") + cacheClient.RLock("foo") if !c.rlockCalled { t.Fatal("should be called") } @@ -91,7 +85,7 @@ func TestCacheRPC(t *testing.T) { } // Test RUnlock - client.RUnlock("foo") + cacheClient.RUnlock("foo") if !c.runlockCalled { t.Fatal("should be called") } diff --git a/packer/rpc/client.go b/packer/rpc/client.go index 73b3ce1d8..6f4bc80b2 100644 --- a/packer/rpc/client.go +++ b/packer/rpc/client.go @@ -31,8 +31,22 @@ func NewClient(rwc io.ReadWriteCloser) (*Client, error) { }, nil } +func (c *Client) Close() error { + if err := c.client.Close(); err != nil { + return err + } + + return c.mux.Close() +} + func (c *Client) Artifact() packer.Artifact { return &artifact{ client: c.client, } } + +func (c *Client) Cache() packer.Cache { + return &cache{ + client: c.client, + } +} diff --git a/packer/rpc/server_new.go b/packer/rpc/server_new.go index 9ad95b6ce..0adcfce5b 100644 --- a/packer/rpc/server_new.go +++ b/packer/rpc/server_new.go @@ -28,6 +28,10 @@ func (s *Server) RegisterArtifact(a packer.Artifact) { s.registerComponent("Artifact", &ArtifactServer{a}, false) } +func (s *Server) RegisterCache(c packer.Cache) { + s.registerComponent("Cache", &CacheServer{c}, false) +} + // ServeConn serves a single connection over the RPC server. It is up // to the caller to obtain a proper io.ReadWriteCloser. func (s *Server) ServeConn(conn io.ReadWriteCloser) {