forked from google/thread-weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManagerTest.java
More file actions
123 lines (102 loc) · 3.31 KB
/
UserManagerTest.java
File metadata and controls
123 lines (102 loc) · 3.31 KB
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
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2009 Weaver authors
*
* This code is part of the Weaver tutorial and may be freely used.
*/
import com.google.testing.threadtester.InterleavedRunner;
import com.google.testing.threadtester.MainRunnableImpl;
import com.google.testing.threadtester.RunResult;
import com.google.testing.threadtester.SecondaryRunnableImpl;
import com.google.testing.threadtester.utils.BlockingProxy;
import junit.framework.TestCase;
import java.util.HashSet;
import java.util.Set;
/**
* Unit test for UserManager. Demonstrates use of
* {@link com.google.testing.threadtester.utils.BlockingProxy}.
*
* NOTE: This test will fail. It was written to demonstrate a fault in the class
* under test.
*
* @author alasdair.mackintosh@gmail.com (Alasdair Mackintosh)
*/
public class UserManagerTest extends TestCase {
private static final String USER = "User";
private class FakeDatabase implements Database {
private Set<String> users = new HashSet<String>();
@Override
public boolean userExists(String username) {
return users.contains(username);
}
@Override
public void addUser(String username) {
if (userExists(username)) {
throw new IllegalArgumentException("User exists");
}
users.add(username);
}
}
public void testAddUser() {
// Create a fake DB, and then create a blocking proxy that will block after
// we have invoked db.userExists()
final Database fakeDb = new FakeDatabase();
final BlockingProxy<Database> dbProxy =
BlockingProxy.create(Database.class, fakeDb, "userExists", false);
// Use an InterleavedRunner to interleave the main and second threads.
// The main thread will break at the proxy's blocking position, and
// the second thread will run.
RunResult result = InterleavedRunner.interleaveAtBreakpoint(
new UserMain(dbProxy.getProxy()), new UserSecond(), dbProxy);
result.throwExceptionsIfAny();
}
/**
* Runs the main thread of execution.
*/
private class UserMain extends MainRunnableImpl<UserManager> {
final Database db;
UserManager manager;
UserMain(Database db) {
this.db = db;
}
@Override
public Class<UserManager> getClassUnderTest() {
return UserManager.class;
}
@Override
public UserManager getMainObject() {
return manager;
}
@Override
public void initialize() {
manager = new UserManager(db);
}
/**
* This is invoked after initialization, to run the main body of the
* test. Because of the proxy that we have specified, we will break
* in the middle of playAsset(), and the second thread will be run..
*/
@Override
public void run() {
System.out.printf("Running main thread %s\n", Thread.currentThread());
manager.addUser(USER);
System.out.printf("Main thread finished\n");
}
}
/**
* Runs the second thread of execution.
*/
private class UserSecond extends
SecondaryRunnableImpl<UserManager, UserMain> {
UserManager manager;
@Override
public void initialize(UserMain main) {
manager = main.getMainObject();
}
@Override
public void run() {
System.out.printf("Running second thread %s\n", Thread.currentThread());
assertFalse(manager.addUser(USER));
System.out.printf("Second thread finished\n");
}
}
}