ns_job unit tests
<% set maxThreads 5 set queueId test_queue_0 # ------------------------------------------------------------------------------ # Return code - succcess. # proc rc.success {} { return 0 } # ------------------------------------------------------------------------------ # Return code - failure # proc rc.failure {} { return 1 } # ------------------------------------------------------------------------------ # boolean true # proc true {} { return 1 } # ------------------------------------------------------------------------------ # boolean false # proc false {} { return 0 } # ------------------------------------------------------------------------------ # Write the message to the page. # proc outputMsg {msg} { ns_adp_puts $msg } # ------------------------------------------------------------------------------ # Write the message to the page and the log. # proc outputMsgLog {msg} { ns_log error $msg ns_adp_puts $msg } # ------------------------------------------------------------------------------ # Busy wait proc - used to test threads. # ns_eval proc foo {value} { set x 0 while {$x < 10000} { incr x } return $value } # ------------------------------------------------------------------------------ # Long Busy wait proc - used to test threads. # ns_eval proc fooLong {value} { set x 0 while {$x < 100000} { incr x } return $value } # ------------------------------------------------------------------------------ # Check if the specified queue exists. # proc queueExists {queueId} { set queues [ns_job queues] if {[lsearch -exact $queues $queueId] == -1} { return [false] } return [true] } # ------------------------------------------------------------------------------ # Create the thread pool queue. Also test the create failure cases. # proc createQueue {queueId} { global maxThreads if {[queueExists $queueId]} { # # Trying to create a queue again should throw an error. # if {[catch { ns_job create -desc "Unit Test Queue \#1" $queueId $maxThreads outputMsgLog "Failure: Create queue should have thrown an erorr.
" return [rc.failure] } err]} { return [rc.success] } } else { outputMsgLog "Creating thread pool queue with max thread: $maxThreads ...
" ns_job create -desc "Unit Test Queue \#1" $queueId $maxThreads outputMsgLog "Done creating thread pool queue.

" if {![queueExists $queueId]} { outputMsgLog "Failure: Failed to create queue.
" return [rc.failure] } } return [rc.success] } # ------------------------------------------------------------------------------ # Display Queues # proc displayThreadPool {} { if {[catch { set threadList [ns_job threadlist] } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to get thread pool list. Error Info: $errorInfo
" return [rc.failure] } array set threadArr $threadList outputMsg "\
" outputMsg " " outputMsg "\
ns_job thread pool
maxthreads numthread numidle req
$threadArr(maxthreads) $threadArr(numthreads) $threadArr(numidle) $threadArr(req)

" } # ------------------------------------------------------------------------------ # Display Queues # proc displayQueues {} { if {[catch { set queueList [ns_job queuelist] } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to get queue list. Error Info: $errorInfo
" return [rc.failure] } outputMsg "\
" foreach queue $queueList { array set queueArr $queue outputMsg " " } outputMsg "\
ns_job queue list
name desc maxthread numrunning req
$queueArr(name) $queueArr(desc) $queueArr(maxthreads) $queueArr(numrunning) $queueArr(req)

" } # ------------------------------------------------------------------------------ # Watch queue jobs progress. # proc watchJobs {queueId} { outputMsgLog "Watch $queueId jobs progress ...

" set done 0 while {$done == 0} { set done 1 set first [true] if {[catch { set jobList [ns_job joblist $queueId] } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to get job list info. Error Info: $errorInfo" return [rc.failure] } foreach job $jobList { array set jobArr $job if {$first} { outputMsg "
" set first [false] } outputMsg " " # # Keep watching if any jobs are queued or running. # if {($jobArr(state) == "scheduled") || ($jobArr(state) == "running") } { set done 0 } } outputMsg "
$queueId
id state type request script results time starttime endtime
$jobArr(id) $jobArr(state) $jobArr(type) $jobArr(req) $jobArr(script) $jobArr(results) $jobArr(time) ms [clock format $jobArr(starttime) -format {%D %T}] [clock format $jobArr(endtime) -format {%D %T}]

" displayThreadPool ns_sleep 1 } return [rc.success] } # ------------------------------------------------------------------------------ # Watch queue jobs progress. # proc watchAllJobs {} { outputMsgLog "
Watch all queue's jobs progress ...

" set done 0 while {$done == 0} { set done 1 if {[catch { set queueList [ns_job queues] } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to get queue list. Error Info: $errorInfo" return [rc.failure] } foreach queueId $queueList { set first [true] if {[catch { set jobList [ns_job joblist $queueId] } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to get job list info. Error Info: $errorInfo" return [rc.failure] } foreach job $jobList { array set jobArr $job if {$first} { outputMsg "
" set first [false] } outputMsg " " # # Keep watching if any jobs are queued or running. # if {($jobArr(state) == "scheduled") || ($jobArr(state) == "running") } { set done 0 } } } outputMsg "
$queueId
id state type request script results time starttime endtime
$jobArr(id) $jobArr(state) $jobArr(type) $jobArr(req) $jobArr(script) $jobArr(results) $jobArr(time) ms [clock format $jobArr(starttime) -format {%D %T}] [clock format $jobArr(endtime) -format {%D %T}]


" displayThreadPool ns_sleep 1 outputMsg "
" } return [rc.success] } # ------------------------------------------------------------------------------ # Enqueue jobs # proc enqueueJobs {queueId jobListVar {detached 0}} { upvar $jobListVar jobList set jobList [list] outputMsgLog "Enqueuing jobs ...
" if {[catch { for {set i 0} {$i < 10} {incr i} { if {$detached} { lappend jobList [ns_job queue -detached $queueId "foo $i"] } else { lappend jobList [ns_job queue $queueId "foo $i"] } } } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to enqueue jobs. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "Done enqueuing jobs.

" return [rc.success] } # ------------------------------------------------------------------------------ # Enqueue jobs # proc enqueueLongJobs {queueId jobListVar {detached 0}} { upvar $jobListVar jobList set jobList [list] outputMsgLog "Enqueuing jobs ...
" if {[catch { for {set i 0} {$i < 10} {incr i} { if {$detached} { lappend jobList [ns_job queue -detached $queueId "fooLong $i"] } else { lappend jobList [ns_job queue $queueId "fooLong $i"] } } } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to enqueue jobs. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "Done enqueuing jobs.

" return [rc.success] } # ------------------------------------------------------------------------------ # Wait for each job. # proc waitForEachJob {queueId jobList} { outputMsgLog "Wait for each job to complete ...

" if {[catch { foreach jobId $jobList { outputMsgLog "Job Id: $jobId    \ Job Result: [ns_job wait $queueId $jobId]
" } } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to wait on job. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "

Done removing the jobs.

" return [rc.success] } # ------------------------------------------------------------------------------ # busy wait for all jobs to complete. # proc busyWait {queueId} { set done 0 while {$done == 0} { set done 1 set jobList [ns_job joblist $queueId] foreach job $jobList { array set jobArr $job if {($jobArr(state) == "scheduled") || ($jobArr(state) == "running") } { set done 0 } } } } # ------------------------------------------------------------------------------ # Cancel jobs. # proc cancelJobs {queueId jobList} { outputMsgLog "Cancelling all the jobs...
" foreach jobId $jobList { if {[catch { ns_job cancel $queueId $jobId } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "Failed to cancel a job. \ Job ID: $jobId errorInfo: $errorInfo
" return [rc.failure] } } outputMsgLog "Done cancelling all the jobs.

" return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #1 # proc ns_job_unit_test_1 {testNum} { global queueId set count 10 outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Wait for each job to complete. # if {[waitForEachJob $queueId $jobList]} { return [rc.failure] } return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #2 # proc ns_job_unit_test_2 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Watch jobs progress. # watchJobs $queueId # # Remove the jobs from the queue. # if {[waitForEachJob $queueId $jobList]} { return [rc.failure] } return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #3 # proc ns_job_unit_test_3 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Wait for any job to complete. # outputMsgLog "Wait for any job to complete...
" if {[catch { ns_job waitany $queueId } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "waitany failed. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "Done waiting for any job to complete.

" # # Remove the jobs from the queue. # if {[waitForEachJob $queueId $jobList]} { return [rc.failure] } # # Wait for any job to complete. # outputMsgLog "Wait again for any job (this should not hang) ...
" if {[catch { ns_job waitany $queueId } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "waitany failed. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "Done waiting for any job to complete.

" return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #4 # proc ns_job_unit_test_4 {testNum} { set queueId [ns_job create [ns_job genid]] outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueJobs $queueId jobList [true]] != [rc.success]} { return [rc.failure] } # # Watch jobs progress. # watchJobs $queueId # # Test wait # foreach jobId $jobList { if {[catch { ns_job wait $queueId $jobId outputMsgLog "Failure: Wait should not be allowed for detached jobs.
" return [rc.failure] } err]} { # wait should have thrown an error. } } ns_job delete $queueId return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #5 # proc ns_job_unit_test_5 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Wait for any job to complete. # outputMsgLog "Wait for any job to complete...
" if {[catch { ns_job waitany $queueId } err]} { global errorInfo set savedInfo $errorInfo outputMsgLog "waitany failed. Error Info: $errorInfo" return [rc.failure] } outputMsgLog "Done waiting for any job to complete.

" # # Cancel all the jobs # if {[cancelJobs $queueId $jobList] != [rc.success]} { return [rc.failure] } # # Test wait # foreach jobId $jobList { if {[catch { ns_job wait $queueId $jobId outputMsgLog "Failure: Wait should not be allowed for cancelled jobs.
" return [rc.failure] } err]} { # wait should have thrown an error. } } # # Enqueue jobs # if {[enqueueJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Watch jobs progress. # busyWait $queueId # # Cancel all the jobs # if {[cancelJobs $queueId $jobList] != [rc.success]} { return [rc.failure] } return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #6 # proc ns_job_unit_test_6 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" # # Enqueue jobs # if {[enqueueLongJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Wait for any job to complete. # outputMsgLog "Wait for any job to complete...
" if {[catch { ns_job waitany -timeout 0:10000 $queueId outputMsgLog "Failed to timeout call." return [rc.failure] } err]} { # # When a message times out, it throws an error. # } outputMsgLog "Done waiting for any job to complete.

" # # Cancel all the jobs # if {[cancelJobs $queueId $jobList] != [rc.success]} { return [rc.failure] } # # Enqueue jobs # outputMsgLog "Enqueuing jobs ...
" if {[enqueueLongJobs $queueId jobList] != [rc.success]} { return [rc.failure] } # # Test wait # foreach jobId $jobList { if {[catch { ns_job wait -timeout 0:10000 $queueId $jobId outputMsgLog "Failed to timeout call." return [rc.failure] } err]} { # wait should have thrown an error. } } # # Cancel all the jobs # if {[cancelJobs $queueId $jobList] != [rc.success]} { return [rc.failure] } return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #7 # proc ns_job_unit_test_7 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" set queueId_1 [ns_job genid] set queueId_1_return [ns_job create -description "queueId_1" $queueId_1] set queueId_2 [ns_job genid] set queueId_2_return [ns_job create -description "queueId_2" $queueId_2] # # Enqueue jobs # if {[enqueueLongJobs $queueId_1 jobList_1] != [rc.success]} { return [rc.failure] } # # Enqueue jobs # if {[enqueueJobs $queueId_2_return jobList_2] != [rc.success]} { return [rc.failure] } watchAllJobs # # Wait for each job to complete. # if {[waitForEachJob $queueId_2_return $jobList_2]} { return [rc.failure] } # # Wait for each job to complete. # if {[waitForEachJob $queueId_1_return $jobList_1]} { return [rc.failure] } ns_job delete $queueId_1 ns_job delete $queueId_2_return return [rc.success] } # ------------------------------------------------------------------------------ # Unit Test #8 # proc ns_job_unit_test_8 {testNum} { global queueId outputMsgLog "
Running test # $testNum.

" set queueId_1 [ns_job genid] outputMsgLog "Creating new queue: $queueId_1 ...
" set queueId_1_return [ns_job create -description "queueId_1" $queueId_1] set queueList [ns_job queuelist] set found [false] foreach queue $queueList { array set queueArr $queue if {[string match $queueArr(name) $queueId_1]} { set found [true] } } if {!$found} { outputMsgLog "Failed to create a new queue" return [rc.failure] } outputMsgLog "Done creating queue.

" outputMsgLog "Deleting queue: $queueId_1 ...
" ns_job delete $queueId_1 set queueList [ns_job queuelist] set found [false] foreach queue $queueList { array set queueArr $queue if {[string match $queueArr(name) $queueId_1]} { set found [true] } } if {$found} { outputMsgLog "Failed to delete queue. Queue Id: $queueId_1" return [rc.failure] } outputMsgLog "Done deleting queue.

" return [rc.success] } # ------------------------------------------------------------------------------ # Get the test. # 0 (zero) returned if no test is selected. # proc getTestNum {} { set queryOptions [ns_conn query] set testSelected 0 foreach item $queryOptions { set pair [split $item "="] if {[llength $pair] != 2} { return $testSelected } set opt [lindex $pair 0] set value [lindex $pair 1] if {$opt == "testSelected"} { set testSelected $value } } return $testSelected } # ------------------------------------------------------------------------------ # Display menu # proc displayMenu {} { outputMsg "\ Select a test to run:



" } # ------------------------------------------------------------------------------ # Main # displayMenu if {[createQueue $queueId] == [rc.failure]} { outputMsgLog "Failed to create queue" return [rc.failure] } displayThreadPool displayQueues set testNum [getTestNum] switch -glob -- $testNum { 0 { } 1 { if {[ns_job_unit_test_1 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 2 { if {[ns_job_unit_test_2 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 3 { if {[ns_job_unit_test_3 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 4 { if {[ns_job_unit_test_4 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 5 { if {[ns_job_unit_test_5 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 6 { if {[ns_job_unit_test_6 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 7 { if {[ns_job_unit_test_7 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } 8 { if {[ns_job_unit_test_8 $testNum] != [rc.success]} { outputMsgLog "
Test $testNum failed!
" } else { outputMsgLog "
Test # $testNum Complete.
" } } default { outputMsgLog "Unknown option. $testNum" } } %>