-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC commands and some basic tests for this [GH-1]
- Loading branch information
Showing
3 changed files
with
131 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package packer | ||
|
||
import ( | ||
"cgl.tideland.biz/asserts" | ||
"net" | ||
"net/rpc" | ||
"testing" | ||
) | ||
|
||
type TestCommand struct { | ||
runArgs []string | ||
runCalled bool | ||
runEnv Environment | ||
} | ||
|
||
func (tc *TestCommand) Run(env Environment, args []string) int { | ||
tc.runCalled = true | ||
tc.runArgs = args | ||
tc.runEnv = env | ||
return 0 | ||
} | ||
|
||
func (tc *TestCommand) Synopsis() string { | ||
return "foo" | ||
} | ||
|
||
// This starts a RPC server for the given command listening on the | ||
// given address. The RPC server is ready when "readyChan" receives a message | ||
// and the RPC server will quit when "stopChan" receives a message. | ||
// | ||
// This function should be run in a goroutine. | ||
func testCommandRPCServer(laddr string, command interface{}, readyChan chan int, stopChan <-chan int) { | ||
listener, err := net.Listen("tcp", laddr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Close the listener when we exit so that the RPC server ends | ||
defer listener.Close() | ||
|
||
// Start the RPC server | ||
server := rpc.NewServer() | ||
server.RegisterName("Command", command) | ||
|
||
go func() { | ||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
// If there is an error, just ignore it. | ||
break | ||
} | ||
|
||
go server.ServeConn(conn) | ||
} | ||
}() | ||
|
||
// We're ready! | ||
readyChan <- 1 | ||
|
||
// Block on waiting to receive from the channel | ||
<-stopChan | ||
} | ||
|
||
func TestRPCCommand(t *testing.T) { | ||
assert := asserts.NewTestingAsserts(t, true) | ||
|
||
// Create the command | ||
command := new(TestCommand) | ||
serverCommand := &RPCServerCommand{command} | ||
|
||
// Start the RPC server, and make sure to exit it at the end | ||
// of the test. | ||
readyChan := make(chan int) | ||
stopChan := make(chan int) | ||
defer func() { stopChan <- 1 }() | ||
go testCommandRPCServer(":1234", serverCommand, readyChan, stopChan) | ||
<-readyChan | ||
|
||
// Create the command client over RPC and run some methods to verify | ||
// we get the proper behavior. | ||
client, err := rpc.Dial("tcp", ":1234") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
clientComm := &RPCClientCommand{client} | ||
runArgs := []string{"foo", "bar"} | ||
testEnv := testEnvironment() | ||
exitCode := clientComm.Run(testEnv, runArgs) | ||
synopsis := clientComm.Synopsis() | ||
|
||
assert.Equal(command.runArgs, runArgs, "Correct args should be sent") | ||
assert.Equal(exitCode, 0, "Exit code should be correct") | ||
assert.Equal(synopsis, "foo", "Synopsis should be correct") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters