11import { delay } from "@std/async/delay" ;
2- import { assertEquals , assertRejects } from "@std/assert" ;
2+ import { assertEquals , assertRejects , assertThrows } from "@std/assert" ;
33import { promiseState } from "./promise_state.ts" ;
44import { Notify } from "./notify.ts" ;
55
@@ -8,21 +8,61 @@ Deno.test("Notify", async (t) => {
88 const notify = new Notify ( ) ;
99 const waiter1 = notify . notified ( ) ;
1010 const waiter2 = notify . notified ( ) ;
11+ assertEquals ( notify . waiterCount , 2 ) ;
1112
1213 notify . notify ( ) ;
14+ assertEquals ( notify . waiterCount , 1 ) ;
1315 assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
1416 assertEquals ( await promiseState ( waiter2 ) , "pending" ) ;
1517
1618 notify . notify ( ) ;
19+ assertEquals ( notify . waiterCount , 0 ) ;
1720 assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
1821 assertEquals ( await promiseState ( waiter2 ) , "fulfilled" ) ;
1922 } ) ;
2023
24+ await t . step ( "'notify' wakes up a multiple waiters" , async ( ) => {
25+ const notify = new Notify ( ) ;
26+ const waiter1 = notify . notified ( ) ;
27+ const waiter2 = notify . notified ( ) ;
28+ const waiter3 = notify . notified ( ) ;
29+ const waiter4 = notify . notified ( ) ;
30+ const waiter5 = notify . notified ( ) ;
31+ assertEquals ( notify . waiterCount , 5 ) ;
32+
33+ notify . notify ( 2 ) ;
34+ assertEquals ( notify . waiterCount , 3 ) ;
35+ assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
36+ assertEquals ( await promiseState ( waiter2 ) , "fulfilled" ) ;
37+ assertEquals ( await promiseState ( waiter3 ) , "pending" ) ;
38+ assertEquals ( await promiseState ( waiter4 ) , "pending" ) ;
39+ assertEquals ( await promiseState ( waiter5 ) , "pending" ) ;
40+
41+ notify . notify ( 2 ) ;
42+ assertEquals ( notify . waiterCount , 1 ) ;
43+ assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
44+ assertEquals ( await promiseState ( waiter2 ) , "fulfilled" ) ;
45+ assertEquals ( await promiseState ( waiter3 ) , "fulfilled" ) ;
46+ assertEquals ( await promiseState ( waiter4 ) , "fulfilled" ) ;
47+ assertEquals ( await promiseState ( waiter5 ) , "pending" ) ;
48+
49+ notify . notify ( 2 ) ;
50+ assertEquals ( notify . waiterCount , 0 ) ;
51+ assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
52+ assertEquals ( await promiseState ( waiter2 ) , "fulfilled" ) ;
53+ assertEquals ( await promiseState ( waiter3 ) , "fulfilled" ) ;
54+ assertEquals ( await promiseState ( waiter4 ) , "fulfilled" ) ;
55+ assertEquals ( await promiseState ( waiter5 ) , "fulfilled" ) ;
56+ } ) ;
57+
2158 await t . step ( "'notifyAll' wakes up all waiters" , async ( ) => {
2259 const notify = new Notify ( ) ;
2360 const waiter1 = notify . notified ( ) ;
2461 const waiter2 = notify . notified ( ) ;
62+ assertEquals ( notify . waiterCount , 2 ) ;
63+
2564 notify . notifyAll ( ) ;
65+ assertEquals ( notify . waiterCount , 0 ) ;
2666 assertEquals ( await promiseState ( waiter1 ) , "fulfilled" ) ;
2767 assertEquals ( await promiseState ( waiter2 ) , "fulfilled" ) ;
2868 } ) ;
@@ -69,4 +109,17 @@ Deno.test("Notify", async (t) => {
69109 ) ;
70110 } ,
71111 ) ;
112+
113+ await t . step (
114+ "'notify' throws RangeError if size is not a positive safe integer" ,
115+ ( ) => {
116+ const notify = new Notify ( ) ;
117+ assertThrows ( ( ) => notify . notify ( NaN ) , RangeError ) ;
118+ assertThrows ( ( ) => notify . notify ( Infinity ) , RangeError ) ;
119+ assertThrows ( ( ) => notify . notify ( - Infinity ) , RangeError ) ;
120+ assertThrows ( ( ) => notify . notify ( - 1 ) , RangeError ) ;
121+ assertThrows ( ( ) => notify . notify ( 1.1 ) , RangeError ) ;
122+ assertThrows ( ( ) => notify . notify ( 0 ) , RangeError ) ;
123+ } ,
124+ ) ;
72125} ) ;
0 commit comments