From 00fd91157999070af7602e7b2b6bec91ceceb353 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 19 Mar 2018 18:00:49 -0700 Subject: [PATCH] truly fix streaming gap issue, remove sleeps from tests --- routes/_actions/streaming.js | 6 +++- routes/_store/observers/timelineObservers.js | 22 ++++++------ tests/spec/105-deletes.js | 35 +++++++++----------- tests/spec/107-streaming-gap.js | 4 +-- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/routes/_actions/streaming.js b/routes/_actions/streaming.js index 7f181b74..56337aa2 100644 --- a/routes/_actions/streaming.js +++ b/routes/_actions/streaming.js @@ -21,7 +21,8 @@ function processMessage (instanceName, timelineName, message) { stop('processMessage') } -export function createStream (streamingApi, instanceName, accessToken, timelineName) { +export function createStream (streamingApi, instanceName, accessToken, + timelineName, onOpenStream) { return new TimelineStream(streamingApi, accessToken, timelineName, { onMessage (msg) { if (msg.event !== 'update' && msg.event !== 'delete' && msg.event !== 'notification') { @@ -33,6 +34,9 @@ export function createStream (streamingApi, instanceName, accessToken, timelineN }) }, onOpen () { + if (onOpenStream) { + onOpenStream() + } console.log('opened stream for timeline', timelineName) }, onClose () { diff --git a/routes/_store/observers/timelineObservers.js b/routes/_store/observers/timelineObservers.js index ca5ec167..5ab81574 100644 --- a/routes/_store/observers/timelineObservers.js +++ b/routes/_store/observers/timelineObservers.js @@ -43,12 +43,12 @@ export function timelineObservers (store) { let accessToken = store.get('accessToken') await updateInstanceInfo(currentInstance) - let checkInstanceAndTimelineAreUnchanged = () => ( + let currentTimelineIsUnchanged = () => ( store.get('currentInstance') === currentInstance && store.get('currentTimeline') === currentTimeline ) - if (!checkInstanceAndTimelineAreUnchanged()) { + if (!currentTimelineIsUnchanged()) { return } @@ -56,23 +56,23 @@ export function timelineObservers (store) { currentTimeline, 'timelineItemIds') let firstTimelineItemId = timelineItemIds && timelineItemIds[0] - if (firstTimelineItemId) { + let onOpenStream = async () => { + if (!firstTimelineItemId || !currentTimelineIsUnchanged()) { + return + } // fill in the "streaming gap" – i.e. fetch the most recent 20 items so that there isn't // a big gap in the timeline if you haven't looked at it in awhile - // TODO: race condition here, should start streaming while this request is ongoing - let newTimelineItems = await getTimeline(currentInstance, accessToken, currentTimeline, - null, firstTimelineItemId) + let newTimelineItems = await getTimeline(currentInstance, accessToken, + currentTimeline, null, firstTimelineItemId) if (newTimelineItems.length) { addStatusesOrNotifications(currentInstance, currentTimeline, newTimelineItems) } - if (!checkInstanceAndTimelineAreUnchanged()) { - return - } } let instanceInfo = store.get('currentInstanceInfo') - currentTimelineStream = createStream(instanceInfo.urls.streaming_api, - currentInstance, accessToken, currentTimeline) + let streamingApi = instanceInfo.urls.streaming_api + currentTimelineStream = createStream(streamingApi, currentInstance, accessToken, + currentTimeline, onOpenStream) if (process.env.NODE_ENV !== 'production') { window.currentTimelineStream = currentTimelineStream diff --git a/tests/spec/105-deletes.js b/tests/spec/105-deletes.js index 5a3c6420..7af353e1 100644 --- a/tests/spec/105-deletes.js +++ b/tests/spec/105-deletes.js @@ -10,59 +10,56 @@ fixture`105-deletes.js` .page`http://localhost:4002` test('deleted statuses are removed from the timeline', async t => { + let timeout = 20000 await t.useRole(foobarRole) .hover(getNthStatus(0)) let status = await postAs('admin', "I'm gonna delete this") - await sleep(1000) - await t.expect(getNthStatus(0).innerText).contains("I'm gonna delete this") + await t.expect(getNthStatus(0).innerText).contains("I'm gonna delete this", {timeout}) await deleteAs('admin', status.id) - await sleep(1000) - await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this") + await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this", {timeout}) await clickToNotificationsAndBackHome(t) - await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this") + await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this", {timeout}) await t.navigateTo('/notifications') await forceOffline() await t.click(homeNavButton) - await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this") + await t.expect(getNthStatus(0).innerText).notContains("I'm gonna delete this", {timeout}) await forceOnline() await t .navigateTo('/') - .expect(getNthStatus(0).innerText).notContains("I'm gonna delete this") + .expect(getNthStatus(0).innerText).notContains("I'm gonna delete this", {timeout}) }) test('deleted statuses are removed from threads', async t => { + let timeout = 20000 await t.useRole(foobarRole) .hover(getNthStatus(0)) let status = await postAs('admin', "I won't delete this") let reply = await postReplyAs('admin', 'But I will delete this', status.id) - await sleep(5000) - await t.expect(getNthStatus(0).innerText).contains('But I will delete this') - .expect(getNthStatus(1).innerText).contains("I won't delete this") + await t.expect(getNthStatus(0).innerText).contains('But I will delete this', {timeout}) + .expect(getNthStatus(1).innerText).contains("I won't delete this", {timeout}) .click(getNthStatus(1)) .expect(getUrl()).contains('/statuses') - .expect(getNthStatus(0).innerText).contains("I won't delete this") - .expect(getNthStatus(1).innerText).contains('But I will delete this') + .expect(getNthStatus(0).innerText).contains("I won't delete this", {timeout}) + .expect(getNthStatus(1).innerText).contains('But I will delete this', {timeout}) await deleteAs('admin', reply.id) - await sleep(1000) await t.expect(getNthStatus(1).exists).notOk() - .expect(getNthStatus(0).innerText).contains("I won't delete this") + .expect(getNthStatus(0).innerText).contains("I won't delete this", {timeout}) await t.navigateTo('/') await forceOffline() await t.click(getNthStatus(0)) .expect(getUrl()).contains('/statuses') .expect(getNthStatus(1).exists).notOk() - .expect(getNthStatus(0).innerText).contains("I won't delete this") + .expect(getNthStatus(0).innerText).contains("I won't delete this", {timeout}) await forceOnline() }) test('deleted statuses result in deleted notifications', async t => { + let timeout = 20000 await t.useRole(foobarRole) .hover(getNthStatus(0)) .expect(notificationsNavButton.getAttribute('aria-label')).eql('Notifications') let status = await postAs('admin', "@foobar yo yo foobar what's up") - await sleep(2000) - await t.expect(notificationsNavButton.getAttribute('aria-label')).eql('Notifications (1)') + await t.expect(notificationsNavButton.getAttribute('aria-label')).eql('Notifications (1)', {timeout}) await deleteAs('admin', status.id) - await sleep(5000) - await t.expect(notificationsNavButton.getAttribute('aria-label')).eql('Notifications') + await t.expect(notificationsNavButton.getAttribute('aria-label')).eql('Notifications', {timeout}) }) diff --git a/tests/spec/107-streaming-gap.js b/tests/spec/107-streaming-gap.js index 0b93f6b9..ac2a2d83 100644 --- a/tests/spec/107-streaming-gap.js +++ b/tests/spec/107-streaming-gap.js @@ -10,7 +10,7 @@ fixture`107-streaming-gap.js` .page`http://localhost:4002` test('fills in a status posted while away from timeline', async t => { - let timeout = 20000 + let timeout = 30000 await t.useRole(foobarRole) .click(localTimelineNavButton) @@ -23,7 +23,7 @@ test('fills in a status posted while away from timeline', async t => { await t.expect(getNthStatus(0).innerText).contains('posted this while you were away!', {timeout}) .click(localTimelineNavButton) .expect(getNthStatus(0).innerText).contains('posted this while you were away!', {timeout}) - await sleep(2000) + .expect(getNthStatus(1).innerText).contains('heyo', {timeout}) await postAs('admin', 'posted this while you were watching') await t.expect(getNthStatus(0).innerText).contains('posted this while you were watching', {timeout}) })