Skip to content

Commit 8d4b004

Browse files
test: add tests for angular 1.4.x and 1.5.x
- add tests using PiCalculator and Angular 1.4.7 - add tests using PiCalculator and Angular 1.5.7 Closes #18
1 parent 45b353c commit 8d4b004

File tree

5 files changed

+356
-6
lines changed

5 files changed

+356
-6
lines changed

dist/angular-workers.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
describe('FredrikSandell.worker-pool', function () {
2+
3+
var WorkerService = null;
4+
var rootScope = null;
5+
6+
beforeEach(function() {
7+
module('FredrikSandell.worker-pool');
8+
inject(function (_WorkerService_, $rootScope) {
9+
WorkerService = _WorkerService_;
10+
rootScope = $rootScope;
11+
});
12+
});
13+
beforeEach(function() {
14+
WorkerService.setAngularUrl('https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js');
15+
16+
});
17+
18+
it('should be an object', function () {
19+
expect(typeof WorkerService).toBe('object');
20+
});
21+
22+
it('should have a method addDependency()', function () {
23+
expect(WorkerService.addDependency).toBeDefined();
24+
expect(typeof WorkerService.addDependency).toBe('function');
25+
});
26+
27+
it('should have a method createAngularWorker()', function () {
28+
expect(WorkerService.createAngularWorker).toBeDefined();
29+
expect(typeof WorkerService.createAngularWorker).toBe('function');
30+
});
31+
32+
it('should have a method setAngularUrl()', function () {
33+
expect(WorkerService.setAngularUrl).toBeDefined();
34+
expect(typeof WorkerService.setAngularUrl).toBe('function');
35+
});
36+
37+
function waitUntilCompletedToTriggerPromiseResolve(completed, rootScope) {
38+
//must wait before triggering digest loop which resolves of the promises
39+
//worker must be given time to initialize
40+
var checker = setInterval(function(){
41+
if(completed) {
42+
clearInterval(checker);
43+
} else {
44+
rootScope.$apply();
45+
}
46+
}, 100);
47+
}
48+
49+
it('createAngularWorker() should return a valid AngularWorker object', function (done) {
50+
var completed = false;
51+
var worker = WorkerService.createAngularWorker(['input', 'output', function (input, output) {
52+
//the worker method does not matter
53+
//should always return a promise resolving to an initialized worker
54+
}]);
55+
worker.then(function(worker) {
56+
expect(typeof worker).toBe('object');
57+
expect(typeof worker.run).toBe('function');
58+
expect(typeof worker.terminate).toBe('function');
59+
}, function(data) {
60+
expect(true).toBe(false); //initialization should be ok
61+
})['finally'](function() {
62+
done();
63+
completed = true;
64+
});
65+
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
66+
});
67+
68+
69+
70+
it('should be possible to pass arguments to worker, send updates from workers, and successfully terminate workers', function (done) {
71+
var completed = false;
72+
var workerPromise = WorkerService.createAngularWorker(['input', 'output', function (input, output) {
73+
/*
74+
This is the body of the worker. This must be self contained. I.e. contain no references to variables
75+
outside of this function body. References to services listed in the dependency list above is of course ok!
76+
The next test provides more details regarding dependencies.
77+
78+
input, is a required dependency for the worker. It should be an object which is serializable through
79+
invocation of JSON.stringify()
80+
81+
output is a required dependency for the worker. It is always a promise which is to be used when communicating
82+
results from the worker. Please not that all objects passed to output should be serializable through
83+
JSON.stringify()
84+
*/
85+
output.notify({status: (input.aNumber+2)});
86+
87+
output.resolve({result: input.aNumber*3});
88+
}]);
89+
//for clarity the promises are not chained
90+
var workerExecutionPromise = workerPromise.then(function(worker) {
91+
//once we reach this point, the worker has its own initialized angular context.
92+
return worker.run({aNumber: 3}); //execute the run method on the fully initialized worker
93+
}, function(data) {
94+
expect(true).toBe(false); //initialization should be ok
95+
});
96+
97+
workerExecutionPromise.then(function success(data) {
98+
expect(data.result).toBe(9);
99+
},function error(reason) {
100+
expect(true).toBe(false); //worker should not fail in this test
101+
}, function update(data) {
102+
expect(data.status).toBe(5);
103+
})['finally'](function() {
104+
done();
105+
completed = true;
106+
});
107+
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
108+
});
109+
110+
it('should be possible reject promise from worker', function (done) {
111+
var completed = false;
112+
var workerPromise = WorkerService.createAngularWorker(['input', 'output', function (input, output) {
113+
output.reject("very bad");
114+
}]).then(function(worker) {
115+
return worker.run({aNumber: 3}); //execute the run method on the fully initialized worker
116+
}).then(function success(data) {
117+
expect(true).toBe(false); //worker should not succeed in this test
118+
},function error(reason) {
119+
expect(reason).toBe("very bad");
120+
})['finally'](function() {
121+
done();
122+
completed = true;
123+
});
124+
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
125+
});
126+
127+
it('should be possible inject angular dependencies in worker', function (done) {
128+
var completed = false;
129+
var workerPromise = WorkerService.createAngularWorker(['input', 'output', '$http', function (input, output, $http) {
130+
//the URL has to be absolute because this is executed based on a blob
131+
output.notify('Getting data from backend');
132+
$http.get('http://localhost:9876/some/url').then(function success() {
133+
//usually we would parse data here. But we don't have a backend:)
134+
}, function error(reason) {
135+
output.notify('Starting to preccess retreived information in a CPU intensive way');
136+
output.resolve(100);
137+
});
138+
}]).then(function(worker) {
139+
return worker.run(); //execute the run method on the fully initialized worker
140+
}).then(function success(data) {
141+
expect(data).toBe(100); //worker should not succeed in this test
142+
})['finally'](function() {
143+
done();
144+
completed = true;
145+
});
146+
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
147+
});
148+
149+
it('should be possible inject custom dependencies in worker', function (done) {
150+
var completed = false;
151+
//the worker runs in its own angular context. It needs to load all dependencies indenpendently of the main
152+
//application. To do this it needs information about how to load the scripts.
153+
//It is important that the URL is absolute!
154+
WorkerService.addDependency(
155+
'PiCalculatorService',
156+
'FredrikSandell.worker-pool-pi-calculator',
157+
'http://localhost:9876/base/src/test/PiCalculator.js'
158+
);
159+
var workerPromise = WorkerService.createAngularWorker(['input', 'output', 'PiCalculatorService', function (input, output, PiCalculatorService) {
160+
output.notify("starting with accuracy: "+input.accuracy);
161+
output.resolve(PiCalculatorService.calculatePi(input.accuracy));
162+
}]).then(function(worker) {
163+
return worker.run({accuracy: 100000}); //execute the run method on the fully initialized worker
164+
}, function error(reason) {
165+
expect(false).toBe(true);
166+
}).then(function success(data) {
167+
expect(data.pi).toBe(3.1416026534897203); //worker should not succeed in this test
168+
})['finally'](function() {
169+
done();
170+
completed = true;
171+
});
172+
waitUntilCompletedToTriggerPromiseResolve(completed, rootScope);
173+
});
174+
175+
});

0 commit comments

Comments
 (0)