From 6878392a9f33c06f4a7c5d17f771e013038a9448 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Thu, 23 Jan 2020 17:16:09 +0100
Subject: [PATCH] bugfix: await for nehubaViewer to become availaable

---
 common/util.js          | 21 +++++++++++++++++++++
 deploy/datasets/util.js | 23 +----------------------
 e2e/src/iv.e2e-spec.js  |  7 +++++--
 e2e/src/ivApi.js        | 14 ++++++++++++++
 e2e/src/util.js         |  4 +++-
 5 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/common/util.js b/common/util.js
index 74a3ff1f2..871481b91 100644
--- a/common/util.js
+++ b/common/util.js
@@ -12,4 +12,25 @@
       return `${kgSchema}/${kgId}`
     }
   }
+
+  const defaultConfig = {
+    timeout: 5000,
+    retries: 3
+  }
+
+  exports.retry = async (fn, { timeout = defaultConfig.timeout, retries = defaultConfig.retries } = defaultConfig) => {
+    let retryNo = 0
+    while (retryNo < retries) {
+      retryNo ++
+      try {
+        const result = await fn()
+        return result
+      } catch (e) {
+        console.warn(`fn failed, retry after ${timeout} milliseconds`)
+        await (() => new Promise(rs => setTimeout(rs, timeout)))()
+      }
+    }
+  
+    throw new Error(`fn failed ${retries} times. Aborting.`)
+  }
 })(typeof exports === 'undefined' ? module.exports : exports)
diff --git a/deploy/datasets/util.js b/deploy/datasets/util.js
index e693ad311..d37ff932f 100644
--- a/deploy/datasets/util.js
+++ b/deploy/datasets/util.js
@@ -3,7 +3,7 @@ const { getCommonSenseDsFilter } = require('./supplements/commonSense')
 const { hasPreview } = require('./supplements/previewFile')
 const path = require('path')
 const fs = require('fs')
-const { getIdFromFullId } = require('../../common/util')
+const { getIdFromFullId, retry } = require('../../common/util')
 
 let getPublicAccessToken
 
@@ -296,27 +296,6 @@ const init = async () => {
   getPublicAccessToken = getPublic
 }
 
-const defaultConfig = {
-  timeout: 5000,
-  retries: 3
-}
-
-const retry = async (fn, { timeout = defaultConfig.timeout, retries = defaultConfig.retries } = defaultConfig) => {
-  let retryNo = 0
-  while (retryNo < retries) {
-    retryNo ++
-    try {
-      const result = await fn()
-      return result
-    } catch (e) {
-      console.warn(`fn failed, retry after ${timeout} milliseconds`)
-      await (() => new Promise(rs => setTimeout(rs, timeout)))()
-    }
-  }
-
-  throw new Error(`fn failed ${retries} times. Aborting.`)
-}
-
 module.exports = {
   getIdFromFullId,
   populateSet,
diff --git a/e2e/src/iv.e2e-spec.js b/e2e/src/iv.e2e-spec.js
index 112580deb..59851d8de 100644
--- a/e2e/src/iv.e2e-spec.js
+++ b/e2e/src/iv.e2e-spec.js
@@ -1,9 +1,11 @@
 const chromeOpts = require('../chromeOpts')
 const noErrorLog = require('./noErrorLog')
-const { getSelectedTemplate, getSelectedParcellation, getSelectedRegions, getCurrentNavigationState } = require('./ivApi')
+const { getSelectedTemplate, getSelectedParcellation, getSelectedRegions, getCurrentNavigationState, awaitNehubaViewer } = require('./ivApi')
 const { getSearchParam, wait } = require('./util')
 const { URLSearchParams } = require('url')
 
+const { waitMultiple } = require('./util')
+
 describe('protractor works', () => {
   it('protractor works', () => {
     expect(true).toEqual(true)
@@ -94,7 +96,8 @@ describe('IAV', () => {
 
       const page = await browser.newPage()
       await page.goto(`${ATLAS_URL}${searchParam}`, { waitUntil: 'networkidle2' })
-      await page.waitFor(500)
+      await awaitNehubaViewer(page)
+      await page.waitFor(500 * waitMultiple)
 
       const actualNav = await getCurrentNavigationState(page)
       expect(expectedNav).toEqual(actualNav)
diff --git a/e2e/src/ivApi.js b/e2e/src/ivApi.js
index 11dfe5505..5da5ce57d 100644
--- a/e2e/src/ivApi.js
+++ b/e2e/src/ivApi.js
@@ -1,3 +1,6 @@
+const { retry } = require('../../common/datasets/util')
+const { waitMultiple } = require('./util')
+
 exports.getSelectedTemplate = (browser) => new Promise((resolve, reject) => {
   browser.executeAsyncScript('let sub = window.interactiveViewer.metadata.selectedTemplateBSubject.subscribe(obj => arguments[arguments.length - 1](obj));sub.unsubscribe()')
     .then(resolve)
@@ -46,4 +49,15 @@ exports.getCurrentNavigationState = async (page) => {
 
     return returnObj
   })
+}
+
+exports.awaitNehubaViewer = async (page) => {
+  const getNVAvailable = () => new Promise((rs, rj) => {
+    page.evaluate(() => {
+      if (nehubaViewer) rs()
+      else rj()
+    })
+  })
+
+  await retry(getNVAvailable, { timeout: 500 * waitMultiple, retries: 10 })
 }
\ No newline at end of file
diff --git a/e2e/src/util.js b/e2e/src/util.js
index 49892e293..149e3edc2 100644
--- a/e2e/src/util.js
+++ b/e2e/src/util.js
@@ -10,4 +10,6 @@ exports.wait = (browser) => new Promise(resolve => {
     
   browser.sleep(1000)
     .then(resolve)
-})
\ No newline at end of file
+})
+
+exports.waitMultiple = process.env.WAIT_ULTIPLE || 1
\ No newline at end of file
-- 
GitLab