Commit 984dd224 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: Cache

parent e9f7a141
...@@ -2,7 +2,6 @@ package rpc ...@@ -2,7 +2,6 @@ package rpc
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net/rpc"
"testing" "testing"
) )
...@@ -52,19 +51,14 @@ func TestCacheRPC(t *testing.T) { ...@@ -52,19 +51,14 @@ func TestCacheRPC(t *testing.T) {
c := new(testCache) c := new(testCache)
// Start the server // Start the server
server := rpc.NewServer() server := NewServer()
RegisterCache(server, c) server.RegisterCache(c)
address := serveSingleConn(server) client := testClient(t, server)
defer client.Close()
// Create the client over RPC and run some methods to verify it works cacheClient := client.Cache()
rpcClient, err := rpc.Dial("tcp", address)
if err != nil {
t.Fatalf("bad: %s", err)
}
client := Cache(rpcClient)
// Test Lock // Test Lock
client.Lock("foo") cacheClient.Lock("foo")
if !c.lockCalled { if !c.lockCalled {
t.Fatal("should be called") t.Fatal("should be called")
} }
...@@ -73,7 +67,7 @@ func TestCacheRPC(t *testing.T) { ...@@ -73,7 +67,7 @@ func TestCacheRPC(t *testing.T) {
} }
// Test Unlock // Test Unlock
client.Unlock("foo") cacheClient.Unlock("foo")
if !c.unlockCalled { if !c.unlockCalled {
t.Fatal("should be called") t.Fatal("should be called")
} }
...@@ -82,7 +76,7 @@ func TestCacheRPC(t *testing.T) { ...@@ -82,7 +76,7 @@ func TestCacheRPC(t *testing.T) {
} }
// Test RLock // Test RLock
client.RLock("foo") cacheClient.RLock("foo")
if !c.rlockCalled { if !c.rlockCalled {
t.Fatal("should be called") t.Fatal("should be called")
} }
...@@ -91,7 +85,7 @@ func TestCacheRPC(t *testing.T) { ...@@ -91,7 +85,7 @@ func TestCacheRPC(t *testing.T) {
} }
// Test RUnlock // Test RUnlock
client.RUnlock("foo") cacheClient.RUnlock("foo")
if !c.runlockCalled { if !c.runlockCalled {
t.Fatal("should be called") t.Fatal("should be called")
} }
......
...@@ -31,8 +31,22 @@ func NewClient(rwc io.ReadWriteCloser) (*Client, error) { ...@@ -31,8 +31,22 @@ func NewClient(rwc io.ReadWriteCloser) (*Client, error) {
}, nil }, 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 { func (c *Client) Artifact() packer.Artifact {
return &artifact{ return &artifact{
client: c.client, client: c.client,
} }
} }
func (c *Client) Cache() packer.Cache {
return &cache{
client: c.client,
}
}
...@@ -28,6 +28,10 @@ func (s *Server) RegisterArtifact(a packer.Artifact) { ...@@ -28,6 +28,10 @@ func (s *Server) RegisterArtifact(a packer.Artifact) {
s.registerComponent("Artifact", &ArtifactServer{a}, false) 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 // ServeConn serves a single connection over the RPC server. It is up
// to the caller to obtain a proper io.ReadWriteCloser. // to the caller to obtain a proper io.ReadWriteCloser.
func (s *Server) ServeConn(conn io.ReadWriteCloser) { func (s *Server) ServeConn(conn io.ReadWriteCloser) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment