Skip to content

Commit 41c2709

Browse files
committed
Add more tests
1 parent cfe97a1 commit 41c2709

File tree

4 files changed

+95
-22
lines changed

4 files changed

+95
-22
lines changed

src/processes/process_queue.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {PID} from 'erlang-types'
2+
3+
class ProcessQueue {
4+
pid: PID
5+
tasks: Function[]
6+
7+
constructor(pid: PID) {
8+
this.pid = pid
9+
this.tasks = []
10+
}
11+
12+
empty() {
13+
return this.tasks.length === 0
14+
}
15+
16+
add(task: any) {
17+
this.tasks.push(task)
18+
}
19+
20+
next() {
21+
return this.tasks.shift()
22+
}
23+
}
24+
25+
export default ProcessQueue

src/processes/scheduler.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
import {PID} from 'erlang-types'
2-
3-
class ProcessQueue {
4-
pid: PID
5-
tasks: Function[]
6-
7-
constructor(pid: PID) {
8-
this.pid = pid
9-
this.tasks = []
10-
}
11-
12-
empty() {
13-
return this.tasks.length === 0
14-
}
15-
16-
add(task: any) {
17-
this.tasks.push(task)
18-
}
19-
20-
next() {
21-
return this.tasks.shift()
22-
}
23-
}
2+
import ProcessQueue from './process_queue'
243

254
class Scheduler {
265
isRunning: boolean

test/mailbox_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import test from 'ava'
2+
import Mailbox from '../src/processes/mailbox'
3+
4+
test('constructor', function(t) {
5+
const mailbox = new Mailbox()
6+
7+
t.is(mailbox.messages.length, 0)
8+
})
9+
10+
test('get', function(t) {
11+
const mailbox = new Mailbox()
12+
13+
t.is(mailbox.get().length, 0)
14+
15+
mailbox.deliver('Hi')
16+
17+
t.is(mailbox.get().length, 1)
18+
})
19+
20+
test('isEmpty', function(t) {
21+
const mailbox = new Mailbox()
22+
23+
t.true(mailbox.isEmpty())
24+
25+
mailbox.deliver('Hi')
26+
27+
t.false(mailbox.isEmpty())
28+
})
29+
30+
test('removeAt', function(t) {
31+
const mailbox = new Mailbox()
32+
33+
mailbox.deliver('Hi')
34+
mailbox.deliver('Goodbye')
35+
36+
mailbox.removeAt(0)
37+
38+
t.is(mailbox.get()[0], 'Goodbye')
39+
})

test/process_queue_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import test from 'ava'
2+
import ProcessQueue from '../src/processes/process_queue'
3+
import {PID} from 'erlang-types'
4+
5+
test('constructor', function(t) {
6+
const pid = new PID()
7+
const queue = new ProcessQueue(pid)
8+
9+
t.is(queue.pid.id, pid.id)
10+
t.is(queue.tasks.length, 0)
11+
t.true(queue.empty())
12+
})
13+
14+
test('add', function(t) {
15+
const pid = new PID()
16+
const queue = new ProcessQueue(pid)
17+
18+
queue.add(() => 'Hi')
19+
t.false(queue.empty())
20+
})
21+
22+
test('next', function(t) {
23+
const pid = new PID()
24+
const queue = new ProcessQueue(pid)
25+
const func = () => 'Hi'
26+
27+
queue.add(func)
28+
29+
t.is(queue.next(), func)
30+
})

0 commit comments

Comments
 (0)