My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
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
102
103
104
105
106
107
108
109
110
111
112
113
package pl.fones.blog.asynctest;

import junit.framework.TestCase;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Base test case for all asynchronous methods.
*
* @author Wojciech Grzeskowiak
* @date 2010/05/11
* @version 1.0
* @link http://blog.fones.pl
*/
public class AsyncBaseTestCase extends TestCase {

// Maximum time that test wait for callback.
private static final int MAX_TIME_FOR_CALLBACK = 5;

// Flags for callbacks appears.
private boolean callbackFlag[];

// Results that was passed to callbacks.
private boolean callbackResult[];

// Triggers for wait/notify methods.
private CountDownLatch latch[];

/**
* Setup callbacks. Creating internal structures and triggers.
* @param methodsCount How many methods can be called asynchronously?
*/
protected void setupCallbacks(int methodsCount) {

latch = new CountDownLatch[methodsCount];
callbackFlag = new boolean[methodsCount];
callbackResult = new boolean[methodsCount];

for (int i = 0; i < methodsCount; ++i) {
latch[i] = new CountDownLatch(1);
callbackFlag[i] = false;
callbackResult[i] = false;
}
}

/**
* Waiting for callback with given id.
* @param id Which callback should this method waiting for.
*/
protected void waitForCallback(int id) {

if (latch == null) {
// If callbacks were not setup first.
throw new IllegalStateException("Please call setupCallbacks first!");
}

try {
// Waiting for callback.
latch[id].await(MAX_TIME_FOR_CALLBACK, TimeUnit.SECONDS);
}
catch(InterruptedException iex) {
// Interrupting. Test fail.
fail("InterruptedException " + iex.toString());
}

synchronized (this) {
// Checking if callback occurs.
assertTrue("There was no callback " + id, callbackFlag[id]);
}
}

/**
* Notify callback with given id and passing result.
* @param id Id of callback to notify.
* @param result Result passed to callback method.
*/
protected void notifyCallback(int id, boolean result) {

if (latch == null) {
// If callbacks were not setup first.
throw new IllegalStateException("Please call setupCallbacks first!");
}

synchronized(this) {
// Callback appears, so set 'true'.
callbackFlag[id] = true;

// What was the result passed in parameters.
callbackResult[id] = result;
}

// Trigger wait method.
latch[id].countDown();
}

/**
* Get callback result.
* @param id Id of callback that result you want to get.
* @return Callback result.
*/
protected boolean getCallbackResult(int id) {

if (latch == null) {
// If callbacks were not setup first.
throw new IllegalStateException("Please call setupCallbacks first!");
}

synchronized(this) {
return callbackResult[id];
}
}
}

Change log

r7 by w.grzeskowiak on Sep 20, 2010   Diff
Move Java project
Go to: 

Older revisions

All revisions of this file

File info

Size: 3405 bytes, 113 lines
Powered by Google Project Hosting