Commit 255b9476 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: Test BuilderFactory result works. Close back RPC after one

conn
parent 5aebe7b1
...@@ -3,7 +3,6 @@ package rpc ...@@ -3,7 +3,6 @@ package rpc
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net/rpc" "net/rpc"
"time"
) )
// An implementation of packer.BuilderFactory where the factory is actually // An implementation of packer.BuilderFactory where the factory is actually
...@@ -39,13 +38,7 @@ func (b *BuilderFactoryServer) CreateBuilder(args *BuilderFactoryCreateArgs, rep ...@@ -39,13 +38,7 @@ func (b *BuilderFactoryServer) CreateBuilder(args *BuilderFactoryCreateArgs, rep
// Now we wrap that back up into a server, and send it on backwards. // Now we wrap that back up into a server, and send it on backwards.
server := NewServer() server := NewServer()
server.RegisterBuilder(builder) server.RegisterBuilder(builder)
server.Start() server.StartSingle()
// Start a timer where we'll quit the server after some number of seconds.
go func() {
time.Sleep(30 * time.Second)
server.Stop()
}()
// Set the reply to the address of the sever // Set the reply to the address of the sever
*reply = server.Address() *reply = server.Address()
......
...@@ -7,6 +7,8 @@ import ( ...@@ -7,6 +7,8 @@ import (
"testing" "testing"
) )
var createResult = &testBuilder{}
type testBuilderFactory struct { type testBuilderFactory struct {
createCalled bool createCalled bool
createName string createName string
...@@ -15,7 +17,7 @@ type testBuilderFactory struct { ...@@ -15,7 +17,7 @@ type testBuilderFactory struct {
func (b *testBuilderFactory) CreateBuilder(name string) packer.Builder { func (b *testBuilderFactory) CreateBuilder(name string) packer.Builder {
b.createCalled = true b.createCalled = true
b.createName = name b.createName = name
return &testBuilder{} return createResult
} }
func TestBuilderFactoryRPC(t *testing.T) { func TestBuilderFactoryRPC(t *testing.T) {
...@@ -37,9 +39,13 @@ func TestBuilderFactoryRPC(t *testing.T) { ...@@ -37,9 +39,13 @@ func TestBuilderFactoryRPC(t *testing.T) {
// Test Create // Test Create
name := "foo" name := "foo"
bClient := &BuilderFactory{client} bClient := &BuilderFactory{client}
_ = bClient.CreateBuilder(name) builder := bClient.CreateBuilder(name)
assert.True(b.createCalled, "create should be called") assert.True(b.createCalled, "create should be called")
assert.Equal(b.createName, "foo", "name should be foo") assert.Equal(b.createName, "foo", "name should be foo")
builder.Prepare(42)
assert.True(createResult.prepareCalled, "prepare should be called")
assert.Equal(createResult.prepareConfig, 42, "42 should be config")
} }
func TestBuilderFactory_ImplementsBuilderFactory(t *testing.T) { func TestBuilderFactory_ImplementsBuilderFactory(t *testing.T) {
......
...@@ -46,6 +46,21 @@ func (s *Server) RegisterUi(ui packer.Ui) { ...@@ -46,6 +46,21 @@ func (s *Server) RegisterUi(ui packer.Ui) {
} }
func (s *Server) Start() error { func (s *Server) Start() error {
return s.start(false)
}
func (s *Server) StartSingle() error {
return s.start(true)
}
func (s *Server) Stop() {
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
}
func (s *Server) start(singleConn bool) error {
if s.listener != nil { if s.listener != nil {
return errors.New("Server already started.") return errors.New("Server already started.")
} }
...@@ -66,15 +81,15 @@ func (s *Server) Start() error { ...@@ -66,15 +81,15 @@ func (s *Server) Start() error {
} }
go s.server.ServeConn(conn) go s.server.ServeConn(conn)
// If we're only accepting a single connection then
// stop.
if singleConn {
s.Stop()
break
}
} }
}(s.listener) }(s.listener)
return nil return nil
} }
func (s *Server) Stop() {
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
}
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