File: test_BatchProcessor.rb

package info (click to toggle)
tj3 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,048 kB
  • sloc: ruby: 36,481; javascript: 1,113; sh: 19; makefile: 17
file content (101 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = test_BatchProcessor.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
#               by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib') if __FILE__ == $0

require 'test/unit'

require 'taskjuggler/BatchProcessor'

class TestProject < Test::Unit::TestCase

  def setup
    @t = Thread.new do
      # Timout for the watchdog timer. Even slow hardware should finish each
      # test in under 30s.
      sleep(120)
      assert(false, 'Test timed out')
    end
  end

  def teardown
    @t.kill
  end

  def test_basic
    doRun(1, 1) { sleep 0.1 }
  end

  def test_simple
    doRun(1, 1) { sleep 0.1 }
    doRun(1, 2) { sleep 0.1 }
    doRun(1, 7) { sleep 0.1 }
    doRun(2, 1) { sleep 0.1 }
    doRun(2, 2) { sleep 0.1 }
    doRun(2, 33) { sleep 0.1 }
    doRun(3, 1) { sleep 0.1 }
    doRun(3, 3) { sleep 0.1 }
    doRun(3, 67) { sleep 0.1 }
  end

  # This test case triggers a Ruby 1.9.x mutex bug
  def test_fileIO
    doRun(3, 80) do
      fname = "test#{$$}.txt"
      f = File.new(fname, 'w')
      0.upto(10000) { |i| f.puts "#{i} Hello, world!" }
      f.close
      File.delete(fname)
    end
  end

  def doRun(maxCPUs, jobs, &block)
    bp = TaskJuggler::BatchProcessor.new(maxCPUs)
    jobs.times do |i|
      bp.queue("job #{i}") { runJob(i, &block) }
    end
    @cnt = 0
    lock = Monitor.new
    bp.wait do |j|
      postprocess(j)
      lock.synchronize { @cnt += 1 }
    end
    assert_equal(jobs, @cnt, "Not all threads terminated propertly (#{@cnt})")
  end

  def runJob(n, &block)
    puts "Job #{n} started"
    yield
    $stderr.puts "Error #{n}" if n % 2 == 0
    puts "Job #{n} finished"
    # Return the job ID as return value
    n
  end

  def postprocess(job)
    assert_equal(job.retVal, job.jobId, 'PID mismatch')
    assert_equal("job #{job.jobId}", job.tag)
    text = <<"EOT"
Job #{job.jobId} started
Job #{job.jobId} finished
EOT
    assert_equal(text, job.stdout, "STDOUT mismatch #{job.stdout}")
    if job.jobId % 2 == 0
      text = "Error #{job.jobId}\n"
    else
      text = ''
    end
    assert_equal(text, job.stderr, "STDERR mismatch #{job.stderr}")
  end
end