01: /*
02: * Copyright (C) 2005-2007 Stephen Ostermiller
03: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
04: *
05: * This program is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * See COPYING.TXT for details.
16: */
17: package com.Ostermiller.util;
18:
19: import java.util.*;
20:
21: /**
22: * Regression test for Parallelizer.
23: * More information about this class is available from <a target="_top" href=
24: * "http://ostermiller.org/utils/Parallelizer.html">ostermiller.org</a>.
25: *
26: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
27: * @since ostermillerutils 1.04.00
28: */
29: class ParallelizerTests {
30: /**
31: * Main method for tests
32: * @param args command line arguments (ignored)
33: */
34: public static void main(String[] args) {
35: try {
36: final HashMap<String, Date> results = new HashMap<String, Date>();
37: final Random random = new Random();
38: Parallelizer pll = new Parallelizer(8);
39: for (int i = 0; i < 100; i++) {
40: final String hashKey = Integer.toString(i);
41: pll.run(new Runnable() {
42: public void run() {
43: try {
44: Thread.sleep(random.nextInt(5000));
45: results.put(hashKey, new Date());
46: } catch (RuntimeException rx) {
47: throw rx;
48: } catch (Exception x) {
49: throw new RuntimeException(x);
50: }
51: }
52: });
53: }
54: if (results.size() == 100)
55: throw new Exception(
56: "Expected results to not yet have 100 items in it.");
57: pll.join();
58: if (results.size() != 100)
59: throw new Exception(
60: "Expected results to have 100 items, not "
61: + results.size());
62: for (int i = 0; i < 100; i++) {
63: String hashKey = Integer.toString(i);
64: Date result = results.get(hashKey);
65: if (result == null)
66: throw new Exception(hashKey + " not in map");
67: }
68: System.exit(0);
69:
70: pll = new Parallelizer();
71: pll.run(new Runnable() {
72: public void run() {
73: throw new RuntimeException("Testing Parallelizer");
74: }
75: });
76:
77: try {
78: pll.join();
79: throw new Exception(
80: "Parallelizer appears not to have thrown expected exception");
81: } catch (RuntimeException rtx) {
82: if (!"Testing Parallelizer".equals(rtx.getMessage())) {
83: throw new Exception(
84: "Expected Testing Parallelizer as message to exception");
85: }
86: }
87:
88: } catch (Throwable x) {
89: x.printStackTrace(System.err);
90: System.exit(1);
91: }
92: }
93: }
|