class TestBed include ClusterFuncs attr_reader :apps,:options,:testbedId def initialize(ops={}) @@testbeds||=[] @@testbeds<100000} @tbedThreads={} ops.each{|k,v|@options[k]=v} @stopped=false @stopping=false @stopMutex=Mutex.new @testbedId=ops[:testbedId] @testbedId||=Crypt::hexdigest(rand.to_s) trap("INT"){ stop! } end def appswarmBaseDir File.join(ENV["HOME"],".appswarm") end def runCluster Appswarm::BASIC_APPS.each{|app|run(app,{})} end def log(*s) TestLogger::log(*s) end def log_line(line) TestLogger::log(line) end def getThread(app) @tbedThreads[app.object_id] end def getApp(type) getApps(type)[0] end def getAppByName(name) @apps.select{|app|app.getAppName==name}[0] end def closeApp(app) if @apps.member?(app) app.stop @apps.delete(app) end end def getGlobalServices @apps.select{|a|a.is_a?(GlobalService)}.map{|a|a.serviceID} end def stop! stop killOtherThreads! end def stop @stopping=true @stopMutex.synchronize { @apps.p_each{|app| log "APP: #{app}" app.quit } log "QUITED" @apps.each{|app| log "Stopping #{app}" begin measureTime { app.stop } rescue Object=>e log"Failed to stop #{app} with error #{e}" log e.backtrace end log "Stopped #{app}" } log "Waiting to shut down" tries=10 while tries>0 and @tbedThreads.select{|k,t|t.alive?}.length>0 sleep 0.2 tries-=1 end log "Joining threads of #{self}" @tbedThreads.each{|k,v| log v.stacktrace } @tbedThreads.each{|k,v| v.kill if v.alive? } @tbedThreads.each{|k,v| v.kill! if v.alive? } @tbedThreads.each{|k,v| log "CRASH:",v.crashtrace if v.crashtrace? } @@testbeds.delete(self) log "Stopped Testbed #{self}" @stopped=true } end def appswarmBaseDir File.join(TestBed.appswarmTestingDir,@testbedId) end def self.appswarmTestingDir File.join(ENV["HOME"],".appswarm_testing") end def self.testbeds @@testbeds||=[] end def self.finishTesting Dir.rmdir_f(appswarmTestingDir) end end FRAG=0.1 def sleepDebug(time) t=0 while t