Permalink
Browse files

converted http json api server to new workshopper

  • Loading branch information...
1 parent 1c7c40c commit b1fa11af303c148543a62e61884b04b5497f3021 @rvagg rvagg committed Feb 28, 2014
@@ -1 +1,97 @@
-// code stuff here
+var through2 = require('through2')
+ , hyperquest = require('hyperquest')
+ , bl = require('bl')
+ , exercise = require('workshopper-exercise')()
+ , filecheck = require('workshopper-exercise/filecheck')
+ , execute = require('workshopper-exercise/execute')
+ , comparestdout = require('workshopper-exercise/comparestdout')
+ , rndport = require('../../lib/rndport')
+
+ , date = new Date(Date.now() - 100000)
+
+
+// the output will be long lines so make the comparison take that into account
+exercise.longCompareOutput = true
+
+// checks that the submission file actually exists
+exercise = filecheck(exercise)
+
+// execute the solution and submission in parallel with spawn()
+exercise = execute(exercise)
+
+
+// set up the data file to be passed to the submission
+exercise.addSetup(function (mode, callback) {
+ this.submissionPort = rndport()
+ this.solutionPort = this.submissionPort + 1
+
+ this.submissionArgs = [ this.submissionPort ]
+ this.solutionArgs = [ this.solutionPort ]
+
+ process.nextTick(callback)
+})
+
+
+// add a processor for both run and verify calls, added *before*
+// the comparestdout processor so we can mess with the stdouts
+exercise.addProcessor(function (mode, callback) {
+ this.submissionStdout.pipe(process.stdout)
+
+ // replace stdout with our own streams
+ this.submissionStdout = through2()
+ if (mode == 'verify')
+ this.solutionStdout = through2()
+
+ setTimeout(query.bind(this, mode), 500)
+
+ process.nextTick(function () {
+ callback(null, true)
+ })
+})
+
+
+// compare stdout of solution and submission
+exercise = comparestdout(exercise)
+
+
+// delayed for 500ms to wait for servers to start so we can start
+// playing with them
+function query (mode) {
+ var exercise = this
+
+ function verify (port, stream) {
+ function error (port, err) {
+ exercise.emit(
+ 'fail'
+ , 'Error connecting to http://localhost:' + port + ': ' + err.message
+ )
+ }
+
+ hyperquest.get('http://localhost:' + port + '/api/parsetime?iso=' + date.toISOString())
+ .on('error', error)
+ .pipe(bl(function (err, data) {
+ if (err)
+ return stream.emit('error', err)
+
+ stream.write(data.toString() + '\n')
+
+ hyperquest.get('http://localhost:' + port + '/api/unixtime?iso=' + date.toISOString())
+ .on('error', error)
+ .pipe(bl(function (err, data) {
+ if (err)
+ return stream.emit('error', err)
+
+ stream.write(data.toString() + '\n')
+ stream.end()
+ }))
+ }))
+ }
+
+ verify(this.submissionPort, this.submissionStdout)
+
+ if (mode == 'verify')
+ verify(this.solutionPort, this.solutionStdout)
+}
+
+
+module.exports = exercise
@@ -1,55 +0,0 @@
-const through = require('through')
- , hyperquest = require('hyperquest')
- , bl = require('bl')
-
- , date = new Date(Date.now() - 100000)
-
-function fetch (port) {
- var output = through()
-
- function error (err) {
- output.write('Error connecting to http://localhost:' + port + ': ' + err.message)
- output.end()
- }
-
- hyperquest.get('http://localhost:' + port + '/api/parsetime?iso=' + date.toISOString())
- .on('error', error)
- .pipe(bl(function (err, data) {
- if (err)
- return output.emit('error', err)
- output.write(data.toString() + '\n')
-
- hyperquest.get('http://localhost:' + port + '/api/unixtime?iso=' + date.toISOString())
- .on('error', error)
- .pipe(bl(function (err, data) {
- if (err)
- return output.emit('error', err)
-
- output.write(data.toString() + '\n')
- output.end()
- }))
-
- }))
- return output
-}
-
-module.exports = function (run) {
- var outputA = through()
- , outputB = through()
- , portA = 1024 + Math.floor(Math.random() * 64511)
- , portB = portA+1
-
- setTimeout(function () {
- fetch(portA).pipe(outputA)
- if (!run)
- fetch(portB).pipe(outputB)
- }, 500)
-
- return {
- submissionArgs : [portA]
- , solutionArgs : [portB]
- , a : outputA
- , b : outputB
- , long : true
- }
-}
@@ -1,34 +0,0 @@
-var http = require('http')
-var url = require('url')
-
-function parsetime (time) {
- return {
- hour: time.getHours(),
- minute: time.getMinutes(),
- second: time.getSeconds()
- }
-}
-
-function unixtime (time) {
- return { unixtime : time.getTime() }
-}
-
-var server = http.createServer(function (req, res) {
- var parsedUrl = url.parse(req.url, true)
- var time = new Date(parsedUrl.query.iso)
- var result
-
- if (/^\/api\/parsetime/.test(req.url))
- result = parsetime(time)
- else if (/^\/api\/unixtime/.test(req.url))
- result = unixtime(time)
-
- if (result) {
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(result))
- } else {
- res.writeHead(404)
- res.end()
- }
-})
-server.listen(Number(process.argv[2]))
@@ -1 +1,34 @@
-// code stuff here
+var http = require('http')
+var url = require('url')
+
+function parsetime (time) {
+ return {
+ hour: time.getHours(),
+ minute: time.getMinutes(),
+ second: time.getSeconds()
+ }
+}
+
+function unixtime (time) {
+ return { unixtime : time.getTime() }
+}
+
+var server = http.createServer(function (req, res) {
+ var parsedUrl = url.parse(req.url, true)
+ var time = new Date(parsedUrl.query.iso)
+ var result
+
+ if (/^\/api\/parsetime/.test(req.url))
+ result = parsetime(time)
+ else if (/^\/api\/unixtime/.test(req.url))
+ result = unixtime(time)
+
+ if (result) {
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(result))
+ } else {
+ res.writeHead(404)
+ res.end()
+ }
+})
+server.listen(Number(process.argv[2]))
@@ -57,7 +57,6 @@ function query (mode) {
var exercise = this
function connect (port, stream) {
- console.log('connect', port)
var input = through2()
, count = 0
, iv

0 comments on commit b1fa11a

Please sign in to comment.