--[[----------------------------------------------------------------------------

WFRxIndexStoreImpUtilsTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2020 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.

------------------------------------------------------------------------------]]

	-- Rx
local Rx = require 'Rx'

local WFRxTestCase = require 'WFRxTestCase'

local WFRxIndexStoreImpUtils = require 'WFRxIndexStoreImpUtils'

local KSDate = require 'KSDate'

--------------------------------------------------------------------------------


--============================================================================--

local WFRxIndexStoreImpUtilsTest = {}

--------------------------------------------------------------------------------

function WFRxIndexStoreImpUtilsTest.rxTestThrottledQuery_throttleTimeRatio( rxTest )
	
	rxTest:setTimeLimit( 30 )
	
	local now = KSDate.timeNowForAnimation
	
	local updateObservable = Rx.ReplaySubject(10)
	
	local counter = 0
	
	local queryFn = function()
		local spinUntil = now() + 1
		while true do
			if now() > spinUntil then
				break
			end
		end
		counter = counter + 1
		return counter
	end
	
	local observer = rxTest:observe( WFRxIndexStoreImpUtils.throttledQuery( updateObservable, 4, queryFn ) )
	
	-- ensure multiple updates are squelched by the time ratio
	updateObservable:onNext( 1 )
	updateObservable:onNext( 1 )
	
	-- query time is 1s + 4s of delay, so wait up to 6s to get the second update
	observer:expectDataWithinTime( 6, 2 )
	
	-- ensure multiple updates are squelched by the time ratio
	updateObservable:onNext( 1 )
	updateObservable:onNext( 1 )
	updateObservable:onNext( 1 )
	updateObservable:onNext( 1 )
	
	-- query time is 1s + 4s of delay, so wait up to 6s to get the third update
	observer:expectDataWithinTime( 6, 3 )
end

return WFRxTestCase.wrapSuite( WFRxIndexStoreImpUtilsTest )
