code cleanups

This commit is contained in:
Axel Kittenberger 2016-12-06 13:19:35 +01:00
parent 815d2c4779
commit 4c77666281
2 changed files with 295 additions and 167 deletions

View File

@ -494,9 +494,9 @@ local Combiner = ( function( )
log( log(
'Delay', 'Delay',
d2.etype, ':',d2.path, d2.etype, ': ',d2.path,
' absorbed by ', ' absorbed by ',
d1.etype,':',d1.path d1.etype,': ',d1.path
) )
return 'absorb' return 'absorb'
@ -509,24 +509,23 @@ local Combiner = ( function( )
local function refi( d1, d2 ) local function refi( d1, d2 )
-- but a directory blocks -- but a directory blocks
if d2.path:byte( -1 ) == 47 then if d2.path:byte( -1 ) == 47
then
log( log(
'Delay', 'Delay',
d2.etype,':',d2.path, d2.etype,': ',d2.path,
' blocked by ', ' blocked by ',
d1.etype,':',d1.path d1.etype,': ',d1.path
) )
return 'stack' return 'stack'
end end
log( log(
'Delay', 'Delay',
d2.etype, ':', d2.path, d2.etype, ': ', d2.path,
' replaces ', ' replaces ',
d1.etype, ':', d1.path d1.etype, ': ', d1.path
) )
return 'replace' return 'replace'
@ -540,9 +539,9 @@ local Combiner = ( function( )
log( log(
'Delay', 'Delay',
d2.etype, ':', d2.path, d2.etype, ': ', d2.path,
' replaces ', ' replaces ',
d1.etype, ':', d1.path d1.etype, ': ', d1.path
) )
return 'replace' return 'replace'
@ -556,9 +555,9 @@ local Combiner = ( function( )
log( log(
'Delay', 'Delay',
d2.etype,':',d2.path, d2.etype,': ',d2.path,
' nullifies ', ' nullifies ',
d1.etype,':',d1.path d1.etype,': ',d1.path
) )
return 'remove' return 'remove'

View File

@ -1,40 +1,48 @@
-- common testing environment -- common testing environment
posix = require('posix') posix = require( 'posix' )
-- escape codes to colorize output on terminal -- escape codes to colorize output on terminal
local c1='\027[47;34m' local c1='\027[47;34m'
local c0='\027[0m' local c0='\027[0m'
--- --
-- writes colorized -- writes colorized
-- --
function cwriteln(...) function cwriteln(...)
io.write(c1, ...) io.write( c1, '++ ', ... )
io.write(c0, '\n') io.write( c0, '\n' )
end end
----- --
-- initializes the pseudo random generator -- initializes the pseudo random generator
-- if environemnt 'SEED' is set, use that as seed. -- if environemnt 'SEED' is set, use that as seed.
local seed = os.getenv('SEED') or os.time() local seed = os.getenv( 'SEED') or os.time( )
math.randomseed(seed)
cwriteln('random seed: ', seed)
----- math.randomseed( seed )
cwriteln( 'random seed: ', seed )
--
-- creates a tmp directory -- creates a tmp directory
-- --
-- @returns the name of the directory -- @returns the name of the directory
-- --
function mktempd() function mktempd( )
local f = io.popen('mktemp -td ltest.XXX', 'r')
local s = f:read('*a') local f = io.popen( 'mktemp -td ltest.XXX', 'r' )
f:close()
s = s:gsub('[\n\r]+', ' ') local s = f:read( '*a' )
s = s:match('^%s*(.-)%s*$')
f:close( )
s = s:gsub( '[\n\r]+', ' ' )
s = s:match( '^%s*(.-)%s*$' )
return s return s
end end
----- --
-- creates a tmp directory with the -- creates a tmp directory with the
-- typical lsyncd test architecture -- typical lsyncd test architecture
-- --
@ -43,94 +51,129 @@ end
-- path of trgdir -- path of trgdir
-- --
function mktemps() function mktemps( )
local tdir = mktempd()..'/' local tdir = mktempd() .. '/'
cwriteln('using ', tdir, ' as test root')
cwriteln( 'using ', tdir, ' as test root' )
local srcdir = tdir..'src/' local srcdir = tdir..'src/'
local trgdir = tdir..'trg/' local trgdir = tdir..'trg/'
posix.mkdir(srcdir)
posix.mkdir(trgdir) posix.mkdir( srcdir )
posix.mkdir( trgdir )
return tdir, srcdir, trgdir return tdir, srcdir, trgdir
end end
---- --
-- Writes a file with 'text' in it. -- Writes a file with 'text' in it
-- and adds a newline. -- and adds a newline.
-- --
function writefile(filename, text) function writefile( filename, text )
local f = io.open(filename, 'w')
if not f then local f = io.open( filename, 'w' )
cwriteln('Cannot open "'..filename..'" for writing.')
if not f
then
cwriteln( 'Cannot open "'..filename..'" for writing.' )
return false return false
end end
f:write(text)
f:write('\n') f:write( text )
f:close()
f:write( '\n' )
f:close( )
return true return true
end end
----- --
-- spawns a subprocess. -- spawns a subprocess.
-- --
-- @returns the processes pid -- @returns the processes pid
-- --
function spawn(...) function spawn(...)
args = {...}
cwriteln('spawning: ', table.concat(args, ' ')) args = { ... }
local pid = posix.fork()
if pid < 0 then cwriteln( 'spawning: ', table.concat( args, ' ' ) )
cwriteln('Error, failed fork!')
os.exit(-1) local pid = posix.fork( )
if pid < 0
then
cwriteln( 'Error, failed fork!' )
os.exit( -1 )
end end
if pid == 0 then
posix.exec(...) if pid == 0
then
posix.exec( ... )
-- should not return -- should not return
cwriteln('Error, failed to spawn: ', ...)
os.exit(-1); cwriteln( 'Error, failed to spawn: ', ... )
os.exit( -1 )
end end
return pid return pid
end end
----- --
-- Makes a lot of random data -- Makes a lot of random data
-- --
-- @param rootdir ... the directory to make data in -- @param rootdir ... the directory to make data in
-- @param n ... roughly how much data action will done -- @param n ... roughly how much data action will done
-- --
function churn(rootdir, n) function churn( rootdir, n )
-- all dirs created, indexed by integer and path
root = {name=''}
alldirs = {root}
dirsWithFileI = {}
dirsWithFileD = {}
----- -- all dirs created, indexed by integer and path
root = { name = '' }
alldirs = { root }
dirsWithFileI = { }
dirsWithFileD = { }
--
-- returns the name of a directory -- returns the name of a directory
-- --
-- name is internal recursive paramter, keep it nil. -- name is internal recursive paramter, keep it nil.
-- --
local function dirname(dir, name) local function dirname( dir, name )
name = name or '' name = name or ''
if not dir then
if not dir
then
return name return name
end end
return dirname(dir.parent, dir.name .. '/' .. name)
return dirname( dir.parent, dir.name .. '/' .. name )
end end
----- --
-- Picks a random dir. -- Picks a random dir.
-- --
local function pickDir(notRoot) local function pickDir( notRoot )
if notRoot then if notRoot
if #alldirs <= 2 then then
if #alldirs <= 2
then
return nil return nil
end end
return alldirs[math.random(2, #alldirs)]
return alldirs[ math.random( 2, #alldirs ) ]
end end
return alldirs[math.random(#alldirs)]
return alldirs[ math.random( #alldirs ) ]
end end
---- --
-- Picks a random file. -- Picks a random file.
-- --
-- Returns 3 values: -- Returns 3 values:
@ -138,188 +181,266 @@ function churn(rootdir, n)
-- * the filename -- * the filename
-- * number of files in directory -- * number of files in directory
-- --
local function pickFile() local function pickFile( )
-- picks the random directory -- picks the random directory
if #dirsWithFileI < 1 then if #dirsWithFileI < 1
then
return return
end end
local rdir = dirsWithFileI[math.random(1, #dirsWithFileI)]
if not rdir then local rdir = dirsWithFileI[ math.random( 1, #dirsWithFileI ) ]
if not rdir
then
return return
end end
-- counts the files in there -- counts the files in there
local c = 0 local c = 0
for name, _ in pairs(rdir) do
if #name == 2 then for name, _ in pairs(rdir)
do
if #name == 2
then
c = c + 1 c = c + 1
end end
end end
-- picks one file at random -- picks one file at random
local cr = math.random(1, c) local cr = math.random( 1, c )
local fn local fn
for name, _ in pairs(rdir) do
if #name == 2 then for name, _ in pairs( rdir )
do
if #name == 2
then
-- filenames are 2 chars wide. -- filenames are 2 chars wide.
cr = cr - 1 cr = cr - 1
if cr == 0 then if cr == 0
then
fn = name fn = name
break break
end end
end end
end end
return rdir, fn, c return rdir, fn, c
end end
----- --
-- Removes a reference to a file -- Removes a reference to a file
-- --
-- @param dir --- directory reference -- @param dir -- directory reference
-- @param fn --- filename -- @param fn -- filename
-- @param c --- number of files in dir -- @param c -- number of files in dir
-- --
local function rmFileReference(dir, fn, c) local function rmFileReference( dir, fn, c )
dir[fn] = nil dir[fn] = nil
if c == 1 then
if c == 1
then
-- if last file from origin dir, it has no files anymore -- if last file from origin dir, it has no files anymore
for i, v in ipairs(dirsWithFileI) do for i, v in ipairs( dirsWithFileI )
if v == dir then do
table.remove(dirsWithFileI, i) if v == dir
then
table.remove( dirsWithFileI, i )
break break
end end
end end
dirsWithFileD[dir] = nil
dirsWithFileD[ dir ] = nil
end end
end end
---- --
-- possible randomized behaviour. -- possible randomized behaviour.
-- just gives it a pause -- just gives it a pause
-- --
local function sleep() local function sleep( )
cwriteln('..zzz..') cwriteln( '..zzz..' )
posix.sleep(1)
posix.sleep( 1 )
end end
---- --
-- possible randomized behaviour. -- possible randomized behaviour.
-- creates a directory -- creates a directory
-- --
local function mkdir() local function mkdir( )
-- chooses a random directory to create it into -- chooses a random directory to create it into
local rdir = pickDir() local rdir = pickDir( )
-- creates a new random one letter name -- creates a new random one letter name
local nn = string.char(96 + math.random(26)) local nn = string.char( 96 + math.random( 26 ) )
if not rdir[nn] then
if not rdir[nn]
then
local ndir = { local ndir = {
name = nn, name = nn,
parent = rdir, parent = rdir,
} }
local dn = dirname(ndir)
rdir[nn] = dn local dn = dirname( ndir )
table.insert(alldirs, ndir)
cwriteln('mkdir '..rootdir..dn) rdir[ nn ] = dn
posix.mkdir(rootdir..dn)
table.insert( alldirs, ndir )
cwriteln( 'mkdir '..rootdir..dn )
posix.mkdir( rootdir..dn )
end end
end end
---- --
-- possible randomized behaviour. -- possible randomized behaviour.
-- Creates a file. -- Creates a file.
-- --
local function mkfile() local function mkfile( )
-- chooses a random directory to create it into -- chooses a random directory to create it into
local rdir = pickDir() local rdir = pickDir()
-- creates a new random one letter name -- creates a new random one letter name
local nn = 'f'..string.char(96 + math.random(26)) local nn = 'f'..string.char( 96 + math.random( 26 ) )
local fn = dirname(rdir) .. nn
cwriteln('mkfile '..rootdir..fn) local fn = dirname( rdir ) .. nn
cwriteln( 'mkfile ' .. rootdir .. fn )
local f = io.open(rootdir..fn, 'w') local f = io.open(rootdir..fn, 'w')
if f then
for i=1,10 do if f
f:write(string.char(96 + math.random(26))) then
for i = 1, 10
do
f:write( string.char( 96 + math.random( 26 ) ) )
end end
f:write('\n')
f:close() f:write( '\n' )
rdir[nn]=true
if not dirsWithFileD[rdir] then f:close( )
table.insert(dirsWithFileI, rdir)
dirsWithFileD[rdir]=true rdir[ nn ]=true
if not dirsWithFileD[ rdir ]
then
table.insert( dirsWithFileI, rdir )
dirsWithFileD[ rdir ]=true
end end
end end
end end
----
-- possible randomized behaviour,
-- moves a directory.
-- --
local function mvdir() -- Possible randomized behaviour:
if #alldirs <= 2 then -- Moves a directory.
--
local function mvdir( )
if #alldirs <= 2
then
return return
end end
-- chooses a random directory to move -- chooses a random directory to move
local odir = pickDir(true) local odir = pickDir( true )
-- chooses a random directory to move to -- chooses a random directory to move to
local tdir = pickDir() local tdir = pickDir( )
-- makes sure tdir is not a subdir of odir -- makes sure tdir is not a subdir of odir
local dd = tdir local dd = tdir
while dd do
if odir == dd then while dd
do
if odir == dd
then
return return
end end
dd = dd.parent dd = dd.parent
end end
-- origin name in the target dir already -- origin name in the target dir already
if tdir[odir.name] ~= nil then if tdir[odir.name] ~= nil
then
return return
end end
local on = dirname(odir)
local tn = dirname(tdir) local on = dirname( odir )
cwriteln('mvdir ',rootdir,on,' -> ',rootdir,tn,odir.name)
os.rename(rootdir..on, rootdir..tn..odir.name) local tn = dirname( tdir )
odir.parent[odir.name] = nil
cwriteln( 'mvdir ', rootdir,on, ' -> ', rootdir, tn, odir.name )
os.rename( rootdir..on, rootdir..tn..odir.name )
odir.parent[ odir.name ] = nil
odir.parent = tdir odir.parent = tdir
tdir[odir.name] = odir
tdir[ odir.name ] = odir
end end
---- --
-- possible randomized behaviour, -- possible randomized behaviour,
-- moves a file. -- moves a file.
-- --
local function mvfile() local function mvfile( )
local odir, fn, c = pickFile() local odir, fn, c = pickFile( )
if not odir then
if not odir
then
return return
end end
-- picks a directory with a file at random -- picks a directory with a file at random
-- picks a target directory at random -- picks a target directory at random
local tdir = pickDir() local tdir = pickDir( )
local on = dirname(odir)
local tn = dirname(tdir)
cwriteln('mvfile ',rootdir,on,fn,' -> ',rootdir,tn,fn)
os.rename(rootdir..on..fn, rootdir..tn..fn)
rmFileReference(odir, fn, c)
tdir[fn] = true local on = dirname( odir )
if not dirsWithFileD[tdir] then
dirsWithFileD[tdir] = true local tn = dirname( tdir )
table.insert(dirsWithFileI, tdir)
cwriteln( 'mvfile ', rootdir, on, fn, ' -> ', rootdir, tn, fn )
os.rename( rootdir..on..fn, rootdir..tn..fn )
rmFileReference( odir, fn, c )
tdir[ fn ] = true
if not dirsWithFileD[ tdir ]
then
dirsWithFileD[ tdir ] = true
table.insert( dirsWithFileI, tdir )
end end
end end
---- --
-- possible randomized behaviour, -- possible randomized behaviour,
-- removes a file. -- removes a file.
-- --
local function rmfile() local function rmfile( )
local dir, fn, c = pickFile()
if dir then local dir, fn, c = pickFile( )
local dn = dirname(dir)
cwriteln('rmfile ',rootdir,dn,fn) if dir
posix.unlink(rootdir..dn..fn) then
rmFileReference(dir, fn, c) local dn = dirname( dir )
cwriteln( 'rmfile ', rootdir, dn, fn )
posix.unlink( rootdir..dn..fn )
rmFileReference( dir, fn, c )
end end
end end
@ -331,19 +452,27 @@ function churn(rootdir, n)
{ 20, rmfile }, { 20, rmfile },
} }
cwriteln('making random data') cwriteln( 'making random data' )
local ndice = 0 local ndice = 0
for i, d in ipairs(dice) do
ndice = ndice + d[1] for i, d in ipairs( dice )
d[1] = ndice do
ndice = ndice + d[ 1 ]
d[ 1 ] = ndice
end end
for ai=1,n do for ai = 1, n
do
-- throws a die what to do -- throws a die what to do
local acn = math.random(ndice) local acn = math.random( ndice )
for i, d in ipairs(dice) do
if acn <= d[1] then for i, d in ipairs( dice )
d[2]() do
if acn <= d[ 1 ]
then
d[ 2 ]( )
break break
end end
end end