Skip to content

Instantly share code, notes, and snippets.

@gerad
Last active August 29, 2015 14:20
Show Gist options
  • Save gerad/950f6aa9dcad0f3dd71f to your computer and use it in GitHub Desktop.
Save gerad/950f6aa9dcad0f3dd71f to your computer and use it in GitHub Desktop.
Example of using contracts to perform integration testing in a pubsub microservices architecture.
[
{
"source": "consumer",
"topic": "/echo",
"payload": "echo"
},
{
"source": "provider",
"topic": "/echo",
"payload": "echo"
}
]
// publish `message` to the "/echo" topic, and call `callback` on response
module.exports = function echo(connection, message, callback) {
connection.subscribe('/echo', callback);
connection.publish('/echo', message);
};
var assert = require('assert');
var contracts = require('contracts'); // our home-built contract testing library
var echo = require('./echo_consumer');
// test the echo consumer
var stubConnection = contracts.connection();
echo(stubConnection, 'echo', function(response) {
assert.equal(response, 'echo');
});
stubConnection.testContract("echo-contract.json", { as: "consumer" });
package echo
type Connection interface {
Subscribe(string, func(string))
Publish(string, string)
}
// echo back any message sent to the "/echo" topic
func StartEchoProvider(connection Connection) {
connection.Subscribe("/echo", func(message string) {
connection.Publish("/echo", message)
})
}
package echo
import (
"testing"
"contracts" // our home-built contract testing library
)
// test the echo provider
func TestEchoProvider(t *testing.T) {
stubConnection := contracts.NewStubConnection()
StartEchoProvider(stubConnection)
stubConnection.TestContract(t, "echo-contract.json", contracts.asProvider)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment