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

WFWorkQueueSchedulerTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2013 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

	-- WF.core.lua.threading
local WFTimer = require 'WFTimer'

	-- WF.core.reactive
local KSReactive = require 'KSReactive'

	-- WF.core.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'

	-- WF.core.utils
local WFWorkQueueScheduler = require 'WFWorkQueueScheduler'


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

local WFWorkQueueSchedulerTest = {}

local workQueueScheduler, nTasksStarted, maxTasksToStart, mostRecentPromiseP

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

function WFWorkQueueSchedulerTest.setUp()

	nTasksStarted = 0
	mostRecentPromiseP = nil

end

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

function WFWorkQueueSchedulerTest.tearDown()

	if workQueueScheduler then
		workQueueScheduler:close()
		workQueueScheduler = nil
	end

end

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

local function startNewWorkFn()

	if nTasksStarted >= maxTasksToStart then
		return nil
	end

	nTasksStarted = nTasksStarted + 1

	mostRecentPromiseP = WFTimer.afterDelayP( .2 )
	
	return mostRecentPromiseP

end

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

function WFWorkQueueSchedulerTest.testHappyPath_emptyQueueAtFirst( test )

	local hasNewWorkB = KSReactive.variableB( false )

	workQueueScheduler = WFWorkQueueScheduler( hasNewWorkB, 2, startNewWorkFn )

	WFAsyncTestCase( test )

		:waitFor( function()
		
			assert( nTasksStarted == 0, "started tasks at wrong time" )
			
			maxTasksToStart = 1
			
			hasNewWorkB:set( true )
			
			assert( nTasksStarted == 1, "didn't start any tasks" )
			
			hasNewWorkB:set( false )
			
			return mostRecentPromiseP
		
		end )
		
		:waitFor( function()
		
			maxTasksToStart = 3
			
			hasNewWorkB:set( true )
			
			assert( nTasksStarted == 3, "didn't start enough tasks at once" )
			
			return mostRecentPromiseP
		
		end )

end

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

function WFWorkQueueSchedulerTest.testDoesntStartTooManyTasks( test )

	local hasNewWorkB = KSReactive.variableB( false )

	workQueueScheduler = WFWorkQueueScheduler( hasNewWorkB, 3, startNewWorkFn )

	WFAsyncTestCase( test )

		:waitFor( function()
		
			assert( nTasksStarted == 0, "started tasks at wrong time" )
			
			maxTasksToStart = 5
			
			hasNewWorkB:set( true )
			
			return tostring( nTasksStarted )
		
		end )
		:continueWhenValueMatches( 'data', '3' )
		
end

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

return WFWorkQueueSchedulerTest
