Last active
August 29, 2015 14:20
-
-
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.
This file contains hidden or 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
[ | |
{ | |
"source": "consumer", | |
"topic": "/echo", | |
"payload": "echo" | |
}, | |
{ | |
"source": "provider", | |
"topic": "/echo", | |
"payload": "echo" | |
} | |
] |
This file contains hidden or 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
// 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); | |
}; |
This file contains hidden or 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
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" }); |
This file contains hidden or 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
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) | |
}) | |
} |
This file contains hidden or 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
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