Skip to content

Commit

Permalink
Clean up documentation and move test workers to separated dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Dec 14, 2010
1 parent 3954e0d commit a8ebf8c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
4 changes: 3 additions & 1 deletion lib/drivers/common.js
Expand Up @@ -59,7 +59,9 @@ function out(data) {
}

/**
* Send _worker_ command to send _data_ to another _worker_.
* Send _worker_ command to send _data_ to another _worker_. Use “:” prefix for
* your own command (for example, “:process”) to avoid conflict with build-in
* runner commands.
*
* @param name {String} worker recipient name
* @param data {Object} any variables, to communicate
Expand Down
7 changes: 2 additions & 5 deletions lib/evoplus-steam.js
Expand Up @@ -147,11 +147,8 @@ evoplus.steam.Runner.prototype = {
},

/**
* Set option for worker. You should use it instead of _option_ argument in
* constructor to set large data of functions (for example, fitness).
*
* Function will be convert to string and send in _func_ command property,
* instead of _value_.
* Set option for worker. Function will be convert to string and send in
* _func_ command property, instead of _value_.
*
* @param name {String} option name
* @param value option value
Expand Down
6 changes: 3 additions & 3 deletions spec/commonSpec.js
Expand Up @@ -3,7 +3,7 @@ describe('drivers/common.js', function() {
afterEach(function () { run.terminate() })

it('should have functions for common drivers tasks', function() {
run = new evoplus.steam.Runner('/__spec__/commoner.js', 1)
run = new evoplus.steam.Runner('/__spec__/workers/commoner.js', 1)
log = []
run.workers[0].onmessage = function(e) { log.push(e.data) }

Expand All @@ -20,7 +20,7 @@ describe('drivers/common.js', function() {
{ command: 'initialized' },
{ command: 'log', data: 'test' },
{ command: 'load', name: 'A', params: { a: 1 } },
{ command: 'worker', to: 'A', content: { command: 'b', b: 2 } },
{ command: 'worker', to: 'A', content: { command: ':b' } },
{ command: 'out', data: { c: 3 } },
{ command: 'log', data: 2 },
{ command: 'log', data: 3 }
Expand All @@ -29,7 +29,7 @@ describe('drivers/common.js', function() {
})

it('should return requested data', function() {
run = new evoplus.steam.Runner('/__spec__/commoner.js', 1)
run = new evoplus.steam.Runner('/__spec__/workers/commoner.js', 1)
run.ready(function() {
var result = []
run.get('answer', function(data) { result.push(data) })
Expand Down
34 changes: 17 additions & 17 deletions spec/runnerSpec.js
Expand Up @@ -3,9 +3,9 @@ describe('evoplus.steam.Runner', function() {
afterEach(function () { run.terminate() })

it('should create and init workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')

expect(run.driver).toEqual('/__spec__/log.js')
expect(run.driver).toEqual('/__spec__/workers/log.js')
expect(run.count).toEqual(2)
expect(run.workers[0]).not.toBeNull()
expect(run.workers[1]).not.toBeNull()
Expand All @@ -31,13 +31,13 @@ describe('evoplus.steam.Runner', function() {
})

it('should load options.count workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 3)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 3)
expect(run.workers[2]).not.toBeNull()
expect(run.count).toEqual(3)
})

it('should send commands to workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 2)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 2)
spyOn(run.workers[0], 'postMessage')
spyOn(run.workers[1], 'postMessage')

Expand All @@ -50,7 +50,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should receive messages from workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')

var received = []
run._onmessage = function(from, msg) { received.push([from, msg]) }
Expand All @@ -64,7 +64,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should dispatch method on message', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 0)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 0)

run._ontest = function() { }
spyOn(run, '_ontest')
Expand All @@ -74,7 +74,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should load another worker by _worker_ command', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')
run._onload('0', { command: 'load', name: 'test', params: { a: 1 } })

expect(run.workers.test).not.toBeNull()
Expand All @@ -95,7 +95,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should allow set listener for workers loading', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')

var callback = jasmine.createSpy()
var result = run.ready(callback)
Expand All @@ -120,13 +120,13 @@ describe('evoplus.steam.Runner', function() {
})

it('should send messages between workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')

run.workers[0].postMessage('clearLog')
run.workers[1].postMessage('clearLog')

run._onworker(0, { command: 'worker', to: 1,
content: { command: 'test', a: 1 } })
content: { command: ':test', a: 1 } })

var log = {}
run._onmessage = function(name, msg) { log[name] = msg }
Expand All @@ -136,12 +136,12 @@ describe('evoplus.steam.Runner', function() {
waitsFor(function() { return log[0] && log[1] })
runs(function() {
expect(log[0]).toEqual([])
expect(log[1]).toEqual([{ command: 'test', from: 0, a: 1 }])
expect(log[1]).toEqual([{ command: ':test', from: 0, a: 1 }])
})
})

it('should have debug log command', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 0)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 0)

spyOn(console, 'log')
run._onlog(0, { data: { a: 1 } })
Expand All @@ -150,7 +150,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should send out commands to listeners', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 0)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 0)

var log = []
run.bind('a', function() { log.push(['a', arguments, this]) })
Expand All @@ -171,7 +171,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should allow to set options to worker', function() {
run = new evoplus.steam.Runner('/__spec__/log.js')
run = new evoplus.steam.Runner('/__spec__/workers/log.js')
run.workers[0].postMessage('clearLog')

var result = run.option('a', { a: 1 })
Expand All @@ -197,7 +197,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should terminate workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 0)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 0)
run.workers[0] = { terminate: function() {} }
run.workers[1] = { terminate: function() {} }

Expand All @@ -213,7 +213,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should start and stop computation', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 1)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 1)
run.workers[0].postMessage('clearLog')

var result = run.start()
Expand All @@ -233,7 +233,7 @@ describe('evoplus.steam.Runner', function() {
})

it('should request data from workers', function() {
run = new evoplus.steam.Runner('/__spec__/log.js', 2)
run = new evoplus.steam.Runner('/__spec__/workers/log.js', 2)
spyOn(run.workers[0], 'postMessage')
spyOn(run.workers[1], 'postMessage')

Expand Down
4 changes: 2 additions & 2 deletions spec/commoner.js → spec/workers/commoner.js
@@ -1,4 +1,4 @@
importScripts('../drivers/common.js')
importScripts('../../drivers/common.js')

init(function(name, command) {
log('I am ' + name + ' from ' + command.count)
Expand All @@ -8,7 +8,7 @@ init(function(name, command) {
on('talk', function(msg) {
log(msg.data)
load('A', { a: 1 })
worker('A', { command: 'b', b: 2 })
worker('A', { command: ':b' })
out({ c: 3 })
log(options.a)
log(options.sum(1, 2))
Expand Down
File renamed without changes.

0 comments on commit a8ebf8c

Please sign in to comment.