Replace JavaScript Standard Style with Prettier.
This commit is contained in:
parent
b9638f2113
commit
d7355e17e1
9 changed files with 647 additions and 675 deletions
|
@ -5,73 +5,73 @@
|
|||
// Test the situation in which two cases of a select can
|
||||
// both end up running. See http://codereview.appspot.com/180068.
|
||||
|
||||
'use strict'
|
||||
"use strict";
|
||||
|
||||
const Channel = require('../../lib')
|
||||
const Channel = require("../../lib");
|
||||
|
||||
it(`doubleselect`, async function () {
|
||||
this.timeout(10 * 1000)
|
||||
const iterations = 100000 // number of iterations
|
||||
it(`doubleselect`, async function() {
|
||||
this.timeout(10 * 1000);
|
||||
const iterations = 100000; // number of iterations
|
||||
|
||||
// sender sends a counter to one of four different channels. If two cases both
|
||||
// end up running in the same iteration, the same value will be sent to two
|
||||
// different channels.
|
||||
const sender = async (n, c1, c2, c3, c4) => {
|
||||
for (let i = 0; i < n; i++) {
|
||||
await Channel.select(c1.push(i), c2.push(i), c3.push(i), c4.push(i))
|
||||
await Channel.select(c1.push(i), c2.push(i), c3.push(i), c4.push(i));
|
||||
}
|
||||
|
||||
c1.close()
|
||||
c2.close()
|
||||
c3.close()
|
||||
c4.close()
|
||||
}
|
||||
c1.close();
|
||||
c2.close();
|
||||
c3.close();
|
||||
c4.close();
|
||||
};
|
||||
|
||||
// mux receives the values from sender and forwards them onto another channel.
|
||||
// It would be simpler to just have sender's four cases all be the same
|
||||
// channel, but this doesn't actually trigger the bug.
|
||||
const mux = async (output, input, done) => {
|
||||
await input.forEach(async (value) => {
|
||||
await output.push(value)
|
||||
})
|
||||
await input.forEach(async value => {
|
||||
await output.push(value);
|
||||
});
|
||||
|
||||
await done.push(true)
|
||||
}
|
||||
await done.push(true);
|
||||
};
|
||||
|
||||
// recver gets a steam of values from the four mux's and checks for
|
||||
// duplicates.
|
||||
const recver = (input) => {
|
||||
const seen = new Map()
|
||||
const recver = input => {
|
||||
const seen = new Map();
|
||||
|
||||
input.forEach((v) => {
|
||||
input.forEach(v => {
|
||||
if (seen.has(v)) {
|
||||
throw new Error(`got duplicate value: ${v}`)
|
||||
throw new Error(`got duplicate value: ${v}`);
|
||||
}
|
||||
|
||||
seen.set(v, true)
|
||||
})
|
||||
}
|
||||
seen.set(v, true);
|
||||
});
|
||||
};
|
||||
|
||||
const c1 = Channel()
|
||||
const c2 = Channel()
|
||||
const c3 = Channel()
|
||||
const c4 = Channel()
|
||||
const done = Channel()
|
||||
const cmux = Channel()
|
||||
sender(iterations, c1, c2, c3, c4)
|
||||
mux(cmux, c1, done)
|
||||
mux(cmux, c2, done)
|
||||
mux(cmux, c3, done)
|
||||
mux(cmux, c4, done)
|
||||
const c1 = Channel();
|
||||
const c2 = Channel();
|
||||
const c3 = Channel();
|
||||
const c4 = Channel();
|
||||
const done = Channel();
|
||||
const cmux = Channel();
|
||||
sender(iterations, c1, c2, c3, c4);
|
||||
mux(cmux, c1, done);
|
||||
mux(cmux, c2, done);
|
||||
mux(cmux, c3, done);
|
||||
mux(cmux, c4, done);
|
||||
|
||||
// We keep the recver because it might catch more bugs in the future.
|
||||
// However, the result of the bug linked to at the top is that we'll
|
||||
// end up panicking with: "throw: bad g->status in ready".
|
||||
recver(cmux)
|
||||
recver(cmux);
|
||||
|
||||
await done.shift()
|
||||
await done.shift()
|
||||
await done.shift()
|
||||
await done.shift()
|
||||
cmux.close()
|
||||
})
|
||||
await done.shift();
|
||||
await done.shift();
|
||||
await done.shift();
|
||||
await done.shift();
|
||||
cmux.close();
|
||||
});
|
||||
|
|
|
@ -4,58 +4,58 @@
|
|||
|
||||
// Test that unbuffered channels act as pure fifos.
|
||||
|
||||
'use strict'
|
||||
"use strict";
|
||||
|
||||
const Channel = require('../../lib')
|
||||
const Channel = require("../../lib");
|
||||
|
||||
it(`fifo`, function () {
|
||||
const N = 10
|
||||
it(`fifo`, function() {
|
||||
const N = 10;
|
||||
|
||||
const AsynchFifo = async () => {
|
||||
const ch = Channel(10)
|
||||
const ch = Channel(10);
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
await ch.push(i)
|
||||
await ch.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
if (await ch.shift() !== i) {
|
||||
throw new Error(`bad receive`)
|
||||
if ((await ch.shift()) !== i) {
|
||||
throw new Error(`bad receive`);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const Chain = async (ch, val, input, output) => {
|
||||
await input.shift()
|
||||
await input.shift();
|
||||
|
||||
if (await ch.shift() !== val) {
|
||||
throw new Error(val)
|
||||
if ((await ch.shift()) !== val) {
|
||||
throw new Error(val);
|
||||
}
|
||||
|
||||
await output.push(1)
|
||||
}
|
||||
await output.push(1);
|
||||
};
|
||||
|
||||
// thread together a daisy chain to read the elements in sequence
|
||||
const SynchFifo = async () => {
|
||||
const ch = Channel()
|
||||
let input = Channel()
|
||||
let start = input
|
||||
const ch = Channel();
|
||||
let input = Channel();
|
||||
let start = input;
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
const output = Channel()
|
||||
Chain(ch, i, input, output)
|
||||
input = output
|
||||
const output = Channel();
|
||||
Chain(ch, i, input, output);
|
||||
input = output;
|
||||
}
|
||||
|
||||
await start.push(0)
|
||||
await start.push(0);
|
||||
|
||||
for (let i = 0; i < N; i++) {
|
||||
await ch.push(i)
|
||||
await ch.push(i);
|
||||
}
|
||||
|
||||
await input.shift()
|
||||
}
|
||||
await input.shift();
|
||||
};
|
||||
|
||||
AsynchFifo()
|
||||
SynchFifo()
|
||||
})
|
||||
AsynchFifo();
|
||||
SynchFifo();
|
||||
});
|
||||
|
|
|
@ -5,29 +5,29 @@
|
|||
// Torture test for goroutines.
|
||||
// Make a lot of goroutines, threaded together, and tear them down cleanly.
|
||||
|
||||
'use strict'
|
||||
"use strict";
|
||||
|
||||
const Channel = require('../../lib')
|
||||
const Channel = require("../../lib");
|
||||
|
||||
it(`goroutines`, async function () {
|
||||
it(`goroutines`, async function() {
|
||||
const f = async (left, right) => {
|
||||
await left.push(await right.shift())
|
||||
}
|
||||
await left.push(await right.shift());
|
||||
};
|
||||
|
||||
const n = 10000
|
||||
const leftmost = Channel()
|
||||
let right = leftmost
|
||||
let left = leftmost
|
||||
const n = 10000;
|
||||
const leftmost = Channel();
|
||||
let right = leftmost;
|
||||
let left = leftmost;
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
right = Channel()
|
||||
f(left, right)
|
||||
left = right
|
||||
right = Channel();
|
||||
f(left, right);
|
||||
left = right;
|
||||
}
|
||||
|
||||
;(async (c) => {
|
||||
await c.push(1)
|
||||
})(right)
|
||||
(async c => {
|
||||
await c.push(1);
|
||||
})(right);
|
||||
|
||||
await leftmost.shift()
|
||||
})
|
||||
await leftmost.shift();
|
||||
});
|
||||
|
|
|
@ -7,72 +7,72 @@
|
|||
// Test various correct and incorrect permutations of send-only,
|
||||
// receive-only, and bidirectional channels.
|
||||
|
||||
'use strict'
|
||||
"use strict";
|
||||
|
||||
const assert = require(`@nodeguy/assert`)
|
||||
const Channel = require('../../lib')
|
||||
const assert = require(`@nodeguy/assert`);
|
||||
const Channel = require("../../lib");
|
||||
|
||||
it(`perm`, function () {
|
||||
const c = Channel()
|
||||
const cr = Channel().readOnly()
|
||||
const cs = Channel().writeOnly()
|
||||
it(`perm`, function() {
|
||||
const c = Channel();
|
||||
const cr = Channel().readOnly();
|
||||
const cs = Channel().writeOnly();
|
||||
|
||||
const n = 0
|
||||
const n = 0;
|
||||
|
||||
assert.throws(() => {
|
||||
Channel.shift(n) // ERROR "receive from non-chan"
|
||||
})
|
||||
Channel.shift(n); // ERROR "receive from non-chan"
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
Channel.push(2, n) // ERROR "send to non-chan"
|
||||
})
|
||||
Channel.push(2, n); // ERROR "send to non-chan"
|
||||
});
|
||||
|
||||
c.push(0) // ok
|
||||
c.shift() // ok
|
||||
c.push(0); // ok
|
||||
c.shift(); // ok
|
||||
|
||||
assert.throws(() => {
|
||||
cr.push(0) // ERROR "send"
|
||||
})
|
||||
cr.push(0); // ERROR "send"
|
||||
});
|
||||
|
||||
cr.shift() // ok
|
||||
cr.shift(); // ok
|
||||
|
||||
cs.push(0) // ok
|
||||
cs.push(0); // ok
|
||||
|
||||
assert.throws(() => {
|
||||
cs.shift() // ERROR "receive"
|
||||
})
|
||||
cs.shift(); // ERROR "receive"
|
||||
});
|
||||
|
||||
Channel.select(
|
||||
c.push(0), // ok
|
||||
c.shift() // ok
|
||||
)
|
||||
c.shift() // ok
|
||||
);
|
||||
|
||||
assert.throws(() => {
|
||||
Channel.select(
|
||||
cr.push(0) // ERROR "send"
|
||||
)
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Channel.select(cr.shift()) // ok
|
||||
Channel.select(cr.shift()); // ok
|
||||
|
||||
Channel.select(cs.push(0)) // ok
|
||||
Channel.select(cs.push(0)); // ok
|
||||
|
||||
assert.throws(() => {
|
||||
Channel.select(cs.shift()) // ERROR "receive"
|
||||
})
|
||||
Channel.select(cs.shift()); // ERROR "receive"
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
cs.forEach(() => {}) // ERROR "receive"
|
||||
})
|
||||
cs.forEach(() => {}); // ERROR "receive"
|
||||
});
|
||||
|
||||
c.close()
|
||||
cs.close()
|
||||
c.close();
|
||||
cs.close();
|
||||
|
||||
assert.throws(() => {
|
||||
cr.close() // ERROR "receive"
|
||||
})
|
||||
cr.close(); // ERROR "receive"
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
Channel.close(n) // ERROR "invalid operation.*non-chan type"
|
||||
})
|
||||
})
|
||||
Channel.close(n); // ERROR "invalid operation.*non-chan type"
|
||||
});
|
||||
});
|
||||
|
|
|
@ -4,70 +4,73 @@
|
|||
|
||||
// Test simple select.
|
||||
|
||||
'use strict'
|
||||
"use strict";
|
||||
|
||||
const Channel = require('../../lib')
|
||||
const Channel = require("../../lib");
|
||||
|
||||
it(`select`, async function () {
|
||||
const closed = Channel()
|
||||
closed.close()
|
||||
let counter = 0
|
||||
let shift = 0
|
||||
it(`select`, async function() {
|
||||
const closed = Channel();
|
||||
closed.close();
|
||||
let counter = 0;
|
||||
let shift = 0;
|
||||
|
||||
const GetValue = () => {
|
||||
counter++
|
||||
return 1 << shift
|
||||
}
|
||||
counter++;
|
||||
return 1 << shift;
|
||||
};
|
||||
|
||||
const Send = async (a, b) => {
|
||||
let done = false
|
||||
let i = 0
|
||||
let done = false;
|
||||
let i = 0;
|
||||
|
||||
do {
|
||||
switch (await Channel.select(a.push(GetValue()), b.push(GetValue()),
|
||||
closed.shift())) {
|
||||
switch (await Channel.select(
|
||||
a.push(GetValue()),
|
||||
b.push(GetValue()),
|
||||
closed.shift()
|
||||
)) {
|
||||
case a:
|
||||
i++
|
||||
a = Channel()
|
||||
break
|
||||
i++;
|
||||
a = Channel();
|
||||
break;
|
||||
|
||||
case b:
|
||||
i++
|
||||
b = Channel()
|
||||
break
|
||||
i++;
|
||||
b = Channel();
|
||||
break;
|
||||
|
||||
default:
|
||||
done = true
|
||||
done = true;
|
||||
}
|
||||
|
||||
shift++
|
||||
} while (!done)
|
||||
shift++;
|
||||
} while (!done);
|
||||
|
||||
return i
|
||||
}
|
||||
return i;
|
||||
};
|
||||
|
||||
let a = Channel(1)
|
||||
let b = Channel(1)
|
||||
let v = await Send(a, b)
|
||||
let a = Channel(1);
|
||||
let b = Channel(1);
|
||||
let v = await Send(a, b);
|
||||
|
||||
if (v !== 2) {
|
||||
throw new Error(`Send returned ${v} !== 2`)
|
||||
throw new Error(`Send returned ${v} !== 2`);
|
||||
}
|
||||
|
||||
const av = await a.shift()
|
||||
const bv = await b.shift()
|
||||
const av = await a.shift();
|
||||
const bv = await b.shift();
|
||||
|
||||
if ((av | bv) !== 3) {
|
||||
throw new Error(`bad values ${av} ${bv}`)
|
||||
throw new Error(`bad values ${av} ${bv}`);
|
||||
}
|
||||
|
||||
v = await Send(a, Channel())
|
||||
v = await Send(a, Channel());
|
||||
|
||||
if (v !== 1) {
|
||||
throw new Error(`Send returned ${v} !== 1`)
|
||||
throw new Error(`Send returned ${v} !== 1`);
|
||||
}
|
||||
|
||||
if (counter !== 10) {
|
||||
throw new Error(`counter is ${counter} !== 10`)
|
||||
throw new Error(`counter is ${counter} !== 10`);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue