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

WFCatTaskManagerTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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

	-- RxLua
local Rx = require 'Rx'

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

local WFRxScheduler = require 'WFRxScheduler'

local WFCatMockCatalog = require 'WFCatMockCatalog'

local WFCatTaskManager = require 'WFCatTaskManager'
local WFCatTask = require 'WFCatTask'

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

local WFCatTaskManagerTest = {}

local kDefaultDescription = "WFCatTask"

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

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

function WFCatTaskManagerTest.rxTestCloseTaskManagerWhenTaskInProgress( rxTest )
	
	local timeOut = 5
	
	rxTest:setTimeLimit( timeOut )
	
	local function actionFn( args, progressReporter, cancelObservable )
		
		progressReporter:setTotal( 10 )
		
		local rxTest = args.rxTest
		
		return rxTest:observe( 
		
			Rx.Observable.timer( 0.5, 1, WFRxScheduler.sharedScheduler() )
			
				:map( function()
					progressReporter:reportSuccess()
				end )
				
				:takeUntil( cancelObservable )
				
				:finally( function()
					progressReporter:done()
				end )
			)
			
	end
	
	-- 1) create a task that waits for 0.5 second, and then report the progress every 1 second
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
		
	local manager = WFCatTaskManager.getCatTaskManager( fakeWFCatalog )
	
	local task = WFCatTask( {
		args = { 
			rxTest = rxTest 
		},
		
		action = actionFn, 
	} )
	
	manager:addToQueue( task )
	
	
	-- 2) close the task manager after 2 seconds
	rxTest:observe( Rx.Observable.timer( 1, WFRxScheduler.sharedScheduler() )
		:map( function()
			fakeWFCatalog:close()
		end )
	)
	
	-- 3) check the result, the task should be cancelled
	local taskObservable = task:taskObservable()
	
	local taskObs = rxTest:observe( taskObservable
		:map( function( data )
			return data
		end )
	)
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0,
		processedCount = 0,
		totalCount = 10,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0.1,
		processedCount = 1,
		totalCount = 10,
	})
	
	taskObs:expectCompleted()
end

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

return WFRxTestCase.wrapSuite( WFCatTaskManagerTest )


