From 431e6cb20ca849a2ee3853f3c1d20457fc8090bd Mon Sep 17 00:00:00 2001
From: Leo Balter <leonardo.balter@gmail.com>
Date: Mon, 19 Mar 2018 19:50:28 -0400
Subject: [PATCH] Add more tests for Atomics wait (#1495)

---
 .../Atomics/wait/cannot-suspend-throws.js     | 25 +++++++++
 .../wait/datablock-bufferdata-throws.js       |  2 +-
 .../Atomics/wait/index-symbol-throws.js       |  2 +-
 .../wait/missing-typedarrayname-throws.js     |  2 +-
 .../Atomics/wait/negative-index-throws.js     |  2 +-
 .../Atomics/wait/not-an-object-throws.js      |  2 +-
 .../Atomics/wait/null-bufferdata-throws.js    |  2 +-
 .../wait/undefined-index-defaults-to-zero.js  |  2 +-
 .../Atomics/wait/was-woken-before-timeout.js  | 55 +++++++++++++++++++
 test/built-ins/Atomics/wait/was-woken.js      |  3 +-
 10 files changed, 89 insertions(+), 8 deletions(-)
 create mode 100644 test/built-ins/Atomics/wait/cannot-suspend-throws.js
 create mode 100644 test/built-ins/Atomics/wait/was-woken-before-timeout.js

diff --git a/test/built-ins/Atomics/wait/cannot-suspend-throws.js b/test/built-ins/Atomics/wait/cannot-suspend-throws.js
new file mode 100644
index 0000000000..c5ec687b29
--- /dev/null
+++ b/test/built-ins/Atomics/wait/cannot-suspend-throws.js
@@ -0,0 +1,25 @@
+// Copyright (C) 2018 Amal Hussein. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-atomics.wait
+description: >
+  Atomics.wait throws if agent cannot be suspended, CanBlock is false
+info: |
+  Assuming [[CanBlock]] is false for the main host.
+
+  Atomics.wait( typedArray, index, value, timeout )
+
+  ... (after args validation)
+  6. Let B be AgentCanSuspend().
+  7. If B is false, throw a TypeError exception.
+  ...
+features: [Atomics, SharedArrayBuffer, TypedArray]
+---*/
+
+var sab = new SharedArrayBuffer(4);
+var int32Array = new Int32Array(sab);
+  
+assert.throws(TypeError, function() {
+  Atomics.wait(int32Array, 0, 0, 0);
+});
diff --git a/test/built-ins/Atomics/wait/datablock-bufferdata-throws.js b/test/built-ins/Atomics/wait/datablock-bufferdata-throws.js
index 88898db582..640550c0b2 100644
--- a/test/built-ins/Atomics/wait/datablock-bufferdata-throws.js
+++ b/test/built-ins/Atomics/wait/datablock-bufferdata-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   Throws a TypeError if typedArray.buffer is not a SharedArrayBuffer
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/index-symbol-throws.js b/test/built-ins/Atomics/wait/index-symbol-throws.js
index 497c0788e6..e391fcc4c3 100644
--- a/test/built-ins/Atomics/wait/index-symbol-throws.js
+++ b/test/built-ins/Atomics/wait/index-symbol-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   Throws a TypeError if index arg can not be converted to an Integer
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/missing-typedarrayname-throws.js b/test/built-ins/Atomics/wait/missing-typedarrayname-throws.js
index db301c5971..7c1fbae1bc 100644
--- a/test/built-ins/Atomics/wait/missing-typedarrayname-throws.js
+++ b/test/built-ins/Atomics/wait/missing-typedarrayname-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   Throws a TypeError if the typedArray arg is not a TypedArray object
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/negative-index-throws.js b/test/built-ins/Atomics/wait/negative-index-throws.js
index 7a8f2310e0..d46d1addba 100644
--- a/test/built-ins/Atomics/wait/negative-index-throws.js
+++ b/test/built-ins/Atomics/wait/negative-index-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   Throws a RangeError is index < 0
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/not-an-object-throws.js b/test/built-ins/Atomics/wait/not-an-object-throws.js
index bcb67dd52e..39d564586c 100644
--- a/test/built-ins/Atomics/wait/not-an-object-throws.js
+++ b/test/built-ins/Atomics/wait/not-an-object-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   Throws a TypeError if typedArray arg is not an Object
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/null-bufferdata-throws.js b/test/built-ins/Atomics/wait/null-bufferdata-throws.js
index 804e0b4d3e..9dc142b0c2 100644
--- a/test/built-ins/Atomics/wait/null-bufferdata-throws.js
+++ b/test/built-ins/Atomics/wait/null-bufferdata-throws.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   A null value for bufferData throws a TypeError
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/undefined-index-defaults-to-zero.js b/test/built-ins/Atomics/wait/undefined-index-defaults-to-zero.js
index 3274028197..c0eb33eb42 100644
--- a/test/built-ins/Atomics/wait/undefined-index-defaults-to-zero.js
+++ b/test/built-ins/Atomics/wait/undefined-index-defaults-to-zero.js
@@ -2,7 +2,7 @@
 // This code is governed by the BSD license found in the LICENSE file.
 /*---
 esid: sec-atomics.wait
-description:
+description: >
   An undefined index arg should translate to 0
 info: |
   Atomics.wait( typedArray, index, value, timeout )
diff --git a/test/built-ins/Atomics/wait/was-woken-before-timeout.js b/test/built-ins/Atomics/wait/was-woken-before-timeout.js
new file mode 100644
index 0000000000..844dc2fe82
--- /dev/null
+++ b/test/built-ins/Atomics/wait/was-woken-before-timeout.js
@@ -0,0 +1,55 @@
+// Copyright (C) 2018 Amal Hussein. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+esid: sec-atomics.wait
+description: >
+  Test that Atomics.wait returns the right result when it was awoken before
+  a timeout
+info: |
+  Atomics.wait( typedArray, index, value, timeout )
+
+  2.Let i be ? ValidateAtomicAccess(typedArray, index).
+    ...
+      2.Let accessIndex be ? ToIndex(requestIndex).
+
+      9.If IsSharedArrayBuffer(buffer) is false, throw a TypeError exception.
+        ...
+          3.If bufferData is a Data Block, return false
+
+          If value is undefined, then
+          Let index be 0.
+features: [Atomics, SharedArrayBuffer, TypedArray]
+---*/
+
+var timeout = 20000;
+$262.agent.start(
+    `
+  $262.agent.receiveBroadcast(function (sab) { 
+    var int32Array = new Int32Array(sab);
+    $262.agent.report(Atomics.wait(int32Array, 0, 0, ${timeout}));
+    $262.agent.leaving();
+  })
+  `);
+  
+  var sab = new SharedArrayBuffer(4);
+  var int32Array = new Int32Array(sab);
+  
+  var sleeping = 100;
+
+  $262.agent.broadcast(int32Array.buffer);
+  $262.agent.sleep(sleeping);
+  
+  assert.sameValue(Atomics.wake(int32Array, 0), 1);
+  
+  assert.sameValue(getReport(), "ok");
+  assert.sameValue(sleeping < timeout, "this test assumes it won't last for more than 20 seconds");
+  
+  function getReport() {
+    var r;
+    while ((r = $262.agent.getReport()) == null) {
+      sleeping += 100;
+      $262.agent.sleep(100);
+    }
+    return r;
+  }
+  
\ No newline at end of file
diff --git a/test/built-ins/Atomics/wait/was-woken.js b/test/built-ins/Atomics/wait/was-woken.js
index 266c4ee0e3..56eeeade10 100644
--- a/test/built-ins/Atomics/wait/was-woken.js
+++ b/test/built-ins/Atomics/wait/was-woken.js
@@ -25,7 +25,8 @@ assert.sameValue(getReport(), "ok");
 
 function getReport() {
   var r;
-  while ((r = $262.agent.getReport()) == null)
+  while ((r = $262.agent.getReport()) == null) {
     $262.agent.sleep(100);
+  }
   return r;
 }
-- 
GitLab