Permalink
Browse files

Final Merge

  • Loading branch information...
1 parent 78a62eb commit 1a477e3c4addab55ea6146f42e83e7b0a3799cc6 Cyrus Hall committed Mar 14, 2007
Showing with 36 additions and 15 deletions.
  1. +19 −8 lib/gosim/data.rb
  2. +3 −2 lib/gosim/simulation.rb
  3. +14 −5 test/simulation_test.rb
View
@@ -20,8 +20,9 @@ def update
end
def reopen
+ puts "tracefile name: #{@tracefile}"
@trace = EventReader.new(@tracefile)
- e = @trace.next
+ e = next_event()
while e[0] == VIEW_MOD
if !@requires_done
begin
@@ -34,7 +35,7 @@ def reopen
load file
end
end
- e = @trace.next
+ e = next_event()
end
@requires_done = true
@@ -48,7 +49,7 @@ def reopen
def queue_to(time)
while time > @time
- event = @trace.next
+ event = next_event()
break if event.nil?
puts "Event read #{event.inspect}"
@@ -64,6 +65,11 @@ def queue_to(time)
return @time
end
+ def next_event
+ @trace.next
+ end
+ private :next_event
+
def handle_data_set_add(event)
if !DataSet[event[0]].nil?
puts "#{event.inspect}"
@@ -76,24 +82,28 @@ class DataSetWriter
include Singleton
def initialize
- @running = false
@last_time = -1
@file = nil
@sim = Simulation::instance
@sim.add_observer(self)
end
+ def running
+ @sim.running
+ end
+ private :running
+
def update
- @file.close
- rewind
+ close
end
def rewind
- @file = Zlib::GzipWriter.open(@output_file)
+ close
+ @file = Zlib::GzipWriter.open(@output_file) if !@output_file.nil?
end
def set_output_file(file = './output/trace.gz')
- if !@running
+ if !running()
@output_file = file
rewind
end
@@ -121,6 +131,7 @@ def log(sym, args)
def close
flush_all
@file.close if !@file.nil?
+ @file = nil
end
end
View
@@ -105,14 +105,15 @@ class Simulation
PORT_NUMBER = 8765
- attr_reader :trace, :time
+ attr_reader :trace, :time, :running
class << self
def run(end_time = 999999999)
Simulation.instance.run(end_time)
end
def reset
+ GoSim::Data::DataSetWriter.instance.close
Simulation.instance.reset
end
@@ -229,7 +230,7 @@ def run(end_time = 2**30)
@time = end_time if @time < end_time
# Make sure to write out all the data files when simulation finishes.
- DataSet.flush_all
+ GoSim::Data::DataSetWriter.instance.flush_all
end
def stop
View
@@ -17,7 +17,8 @@ def initialize(neighbor, num_items)
dir_name = File.join(File.dirname(__FILE__), "output")
Dir.mkdir(dir_name) unless File.exists?(dir_name)
- @dataset = GoSim::DataSet.new(:producer, dir_name)
+ @dataset = GoSim::Data::DataSet.new(:producer)
+ GoSim::Data::DataSetWriter.instance.set_output_file(dir_name + "/trace.gz")
end
def new_item(event)
@@ -57,13 +58,17 @@ def initialize
class TestSimulation < Test::Unit::TestCase
def setup
- @sim = GoSim::Simulation.reset
+ @sim = GoSim::Simulation.instance
@sim.quiet
# turn down logging so we don't see debug messages during unit testing
@sim.trace_log = nil
end
+ def teardown
+ GoSim::Simulation.reset
+ end
+
def test_logging
@sim.trace.level = Logger::FATAL
@sim.quiet
@@ -93,22 +98,25 @@ def test_single_step
def test_data_set
# Test the regular data logging
- file = File.expand_path(File.join(File.dirname(__FILE__), "output", "producer"))
+ file = File.expand_path(File.join(File.dirname(__FILE__), "output", "trace.gz"))
File.delete(file) if File.exists?(file)
consumer = Consumer.new
producer = Producer.new(consumer.sid, 2)
@sim.run
- assert_equal("0: #{producer.sid}, foo\n10: #{producer.sid}, foo\n", IO::read(file))
+
+ ds = GoSim::EventReader.new(file)
+ event = ds.next
+ assert_equal(:time, event[0])
# Now try with an attached handler instead
@sim.reset
consumer = Consumer.new
producer = Producer.new(consumer.sid, 5)
count = 0
- GoSim::DataSet.add_handler(:producer) { count += 1 }
+ GoSim::Data::DataSet.add_handler(:producer) { count += 1 }
@sim.run
assert_equal(5, count)
end
@@ -125,5 +133,6 @@ def test_sim_run
sim_timer = TimerOuter.new
@sim.run(3 * TimerOuter::TIMEOUT_TIME)
assert_equal(3, sim_timer.timer_count)
+
end
end

0 comments on commit 1a477e3

Please sign in to comment.