User:Devon McCormick/mutex TCs.ijs

From J Wiki
Jump to: navigation, search

Test cases for User:Devon McCormick/mutex.ijs, organized under this page. The "TC" functions check the most basic, static functionality. However, "startHRs" is the most realistic test as this starts multiple J processes which all hold and release the same file, recording the timestamps before and after the hold. Subsequent analysis of these timestamps should reveal if there was any overlapping hold. Each process writes out the timestamp info to a ".dat" file using "3!:1"; use "3!:2" on the contents of each file to reconstruct the variable.

NB.* mutex_TCs.ijs: test cases for mutex.ijs.

coclass 'mutexTest'
require '~Code/mutex.ijs'
coinsert 'mutex'

checkIfSelfLocked=: 3 : '(getMUP getMyInfo '''')-:getMUP y'
endSlash=: ],'\'-.{:
wait=: 6!:3

TC0=: 3 : 0
   'fid myi'=. getHold ''
   assert. -. 0*./ . =#&>LFINFO
   releaseHold ''
   assert.    0*./ . =#&>LFINFO
)

TC1=: 3 : 0
   'fid myi'=. getHold ''
   assert. checkIfSelfLocked fread fid
   releaseHold ''
)

TC2=: 3 : 0
   'fid myi'=. getHold ''
   assert. checkIfSelfLocked myi
   releaseHold ''
)

TC3=: 3 : 0
   'fid0 myi0'=. getHold ''
   'fid1 myi1'=. getHold ''
   assert. fid0-:fid1
   assert. checkIfSelfLocked myi0
   assert. checkIfSelfLocked myi1
   releaseHold ''
)

TC4=: 3 : 0
   'fid myi'=. getHold ''
   assert. fid=>0{LFINFO
   assert. fid e. ;whlocks ''
   assert. 3=#fid (]{~[i.~[:;0{"1]) whlocks ''
   assert. LFINFO-:fid (]{~[i.~[:;0{"1]) whfiles ''
   releaseHold ''
)

NB.* holdAndWait: hold file for a random time, then release.
holdAndWait=: 3 : 0
   tm=. tm,qts'' [ 'fid myi'=. getHold '' [ tm=. ,:qts ''
   rc=. *./checkIfSelfLocked &> myi;fread fid
   rc=. rc,wait >:?y
   releaseHold ''
   rc;tm       NB. If self-held, holding time; when asked and got hold.
)

NB.* holdsAndReleases: do number of holds and releases, waiting between each.
holdsAndReleases=: 3 : 0
   'ni wl mhl'=. y  NB. Number of iterations, wait length, max hold length.
   rc=. 0 2$0 2;2 7$2
   while. _1<ni=. <:ni do. wait wl [ rc=. rc,holdAndWait mhl end.
   rc
)

NB.* startHRs: start holders and releasers as separate processes.
startHRs=: 3 : 0
   if. 0=#y do.
       args=. |.(10+|.i.10),. 2 2,2 3,3 2,3 3,2 4,4 2,4 4,2 5,5 2,:5 5
       dd=. 'C:\amisc\J\Parallel\ExampleProblems\Holds\'
   else. 'dd args'=. y end.
   start1HR"1 dd;"1 args
)

start1HR=: 3 : 0
   'rundir arg'=. y
   1!:44 rundir=. endSlash rundir [ svdir=. 1!:43 ''   NB. Run in target dir.
   basis=. fread jpath '~Code/mutex_TCs.ijs'      NB. Get base code.
   rndnm=. 8 (]{~[:?[$[:#]) 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
NB. Obfuscate rand name place-holder to avoid replacement here.
   rplargs=. '{',&.>('RN';'arg';'rundir'),&.>'}'
   addon=. (,rplargs,.rndnm;(":arg);rundir) stringreplace addon
   if. fexist hrfl=. 'HR_',rndnm,'.ijs' do. ferase hrfl end.
   (CR-.~basis,addon) fwrite hrfl                 NB. Create unique HR file.
NB. Set the full path name for the J executable to be invoked here:
   exe=. (<'"',(jpath '~bin'),'\j.exe" -jijx "'),&.><endSlash rundir
NB. The "-jijx" flag avoids opening a session window.
   fork&.>exe,&.>(<hrfl),&.>'"'                   NB. Run HR file.
   1!:44 svdir
)

addon=: 0 : 0
varnm=. 'rctm{RN}'
(varnm)=: holdsAndReleases {arg}
(varnm)=: ('{RN}';(getpid ''),qts''),".varnm
(3!:1 (".varnm)) 1!:2 <'{rundir}',varnm,'.dat'
2!:55 ''
)

The modified script launched as a separate process would look like the above but with additional, final lines customized per task like the ones shown in this example:

varnm=. 'rctmXOZNFNQJ'
(varnm)=: holdsAndReleases 65 4 2
(varnm)=: ('XOZNFNQJ';(getpid ''),qts''),".varnm
(3!:1 (".varnm)) 1!:2 <'C:\amisc\J\Parallel\ExampleProblems\Holds\',varnm,'.dat'
2!:55 ''