Skip to content

Commit 01dc89d

Browse files
committed
Added Semaphore test/example
#39
1 parent bbc6fb6 commit 01dc89d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
3+
* and others contributors as indicated
4+
* by the @authors tag. All rights reserved.
5+
* See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
* This copyrighted material is made available to anyone wishing to use,
8+
* modify, copy, or redistribute it subject to the terms and conditions
9+
* of the GNU Lesser General Public License, v. 2.1.
10+
* This program is distributed in the hope that it will be useful, but WITHOUT A
11+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12+
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13+
* You should have received a copy of the GNU Lesser General Public License,
14+
* v.2.1 along with this distribution; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
* MA 02110-1301, USA.
17+
*
18+
* (C) 1990-2008,
19+
*/
20+
21+
package org.javasim.tests;
22+
23+
import org.javasim.RestartException;
24+
import org.javasim.Semaphore;
25+
import org.javasim.SimulationEntity;
26+
import org.javasim.streams.ExponentialStream;
27+
import org.junit.Test;
28+
29+
import static org.junit.Assert.*;
30+
31+
class DummyEntity extends SimulationEntity
32+
{
33+
public DummyEntity (double mean)
34+
{
35+
InterArrivalTime = new ExponentialStream(mean);
36+
}
37+
38+
public void run ()
39+
{
40+
try
41+
{
42+
hold(InterArrivalTime.getNumber());
43+
}
44+
catch (final Exception ex)
45+
{
46+
}
47+
}
48+
49+
private ExponentialStream InterArrivalTime;
50+
}
51+
52+
53+
public class SemaphoreUnitTest
54+
{
55+
@Test
56+
public void test () throws Exception
57+
{
58+
Semaphore sem = new Semaphore(2);
59+
DummyEntity e1 = new DummyEntity(10);
60+
DummyEntity e2 = new DummyEntity(20);
61+
DummyEntity e3 = new DummyEntity(30);
62+
63+
assertTrue(sem.numberWaiting() == 0);
64+
65+
Semaphore.Outcome result = sem.get(e1);
66+
67+
assertTrue(result == Semaphore.Outcome.DONE);
68+
69+
result = sem.get(e2);
70+
71+
assertTrue(result == Semaphore.Outcome.DONE);
72+
73+
result = sem.tryGet(e3);
74+
75+
assertTrue(result == Semaphore.Outcome.WOULD_BLOCK);
76+
77+
result = sem.get(e3);
78+
79+
assertTrue(result == Semaphore.Outcome.DONE);
80+
assertTrue(sem.numberWaiting() == 1);
81+
}
82+
}

0 commit comments

Comments
 (0)