Skip to content

Instantly share code, notes, and snippets.

@kjlape
Created March 3, 2018 18:01
Show Gist options
  • Select an option

  • Save kjlape/4d90529b5593d36bd5cf3ad21af71e2a to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/4d90529b5593d36bd5cf3ad21af71e2a to your computer and use it in GitHub Desktop.
Pure node.js Monkey Business client
const dgram = require('dgram')
const assert = require('assert');
const SERVER_IP_ADDRESS = '127.0.0.1'
const SERVER_PORT = 6789
function MonkeyBusinessTester(pipe) {
this.pipe = pipe
}
MonkeyBusinessTester.prototype.test = function () {
this.pipe.open()
.connect()
.wait()
.train()
.wait()
.quit()
.disconnect()
.wait()
.close()
}
function Pipe(socket) {
this.socket = socket
}
Pipe.prototype = {
open: function () {
const openPipe = new OpenPipe(this.socket)
return openPipe
}
}
function OpenPipe(socket) {
this.socket = socket
this.promise = Promise.resolve()
}
OpenPipe.prototype = {
send: function (message) {
this.promise = this.promise.then(() =>
new Promise(
(resolve, reject) => {
this.socket.send(
message,
SERVER_PORT,
SERVER_IP_ADDRESS,
(error) => {
if (error) {
reject()
} else {
resolve()
}
}
)
}
)
)
return this
},
receive: function (handler) {
this.promise = this.promise.then(() => new Promise(resolve => {
this.socket.once('message', message => {
handler.call(message)
resolve(message)
})
}
)
)
return this
},
wait: function () {
return this.receive(() => {})
},
connect: function () {
return this.send('CONNECT')
},
train: function () {
return this.send('TRAIN')
},
quit: function () {
return this.send('QUIT')
},
disconnect: function () {
return this.send('DISCONNECT')
},
close: function () {
this.promise = this.promise.then(
() => this.socket.close(),
(error) => {
this.socket.close()
console.log('SOCKET ERROR: ', error)
}
)
return new Pipe(this.socket)
}
}
/***** TESTS *****/
testMonkeyBusinessTester_test()
testMonkeyMessenger_start()
new MonkeyBusinessTester(new Pipe(dgram.createSocket('udp4'))).test()
// testBarrelOMonkeys_API()
function testMonkeyBusinessTester_test() {
const context = {}
const client = {}
const clientSpy = () => spy({ result: client, context })
Object.assign(
client,
{
open: clientSpy(),
connect: clientSpy(),
train: clientSpy(),
disconnect: clientSpy(),
close: clientSpy(),
wait: clientSpy(),
quit: clientSpy(),
receive: clientSpy()
}
)
const subject = new MonkeyBusinessTester(client)
subject.test()
assert.deepEqual(context.calls.map((call) => call.spy),
[
client.open,
client.connect,
client.wait,
client.train,
client.wait,
client.quit,
client.disconnect,
client.wait,
client.close
]
)
}
function testMonkeyMessenger_start() {
const socket = {
bind: spy()
}
const subject = new Pipe(socket)
const started = subject.open()
assert(started)
}
/***** TEST SUPPORT *****/
function spy({ result, context } = {}) {
if (result && result.call) {
result = result.call()
}
const spy = (...params) => {
context && context.calls.push({ params, spy })
spy.calls.push({ params, spy })
return result
}
context && (context.calls = [])
spy.calls = []
return spy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment