Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
tom
2014-09-13 12:00:57 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
931af4e6160ecc6c0b83bc084159d66211f6bed2
931af4e6
1 parent
e985f7e0
Added unit test utilities and some more unit tests.
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
254 additions
and
5 deletions
core/api/pom.xml
core/api/src/test/java/org/onlab/onos/event/AbstractEventAccumulatorTest.java
core/pom.xml
pom.xml
utils/junit/pom.xml
utils/junit/src/main/java/org/onlab/junit/TestTools.java
utils/junit/src/main/javadoc/org/onlab/junit/package.html
utils/junit/src/test/java/org/onlab/junit/TestToolsTest.java
utils/pom.xml
core/api/pom.xml
View file @
931af4e
...
...
@@ -21,10 +21,6 @@
<groupId>
com.google.guava
</groupId>
<artifactId>
guava-testlib
</artifactId>
</dependency>
<dependency>
<groupId>
org.onlab.onos
</groupId>
<artifactId>
onlab-misc
</artifactId>
</dependency>
</dependencies>
</project>
...
...
core/api/src/test/java/org/onlab/onos/event/AbstractEventAccumulatorTest.java
0 → 100644
View file @
931af4e
package
org
.
onlab
.
onos
.
event
;
import
org.junit.Test
;
import
java.util.List
;
import
java.util.Timer
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
onlab
.
junit
.
TestTools
.
delay
;
import
static
org
.
onlab
.
onos
.
event
.
TestEvent
.
Type
.
FOO
;
/**
* Tests the operation of the accumulator.
*/
public
class
AbstractEventAccumulatorTest
{
private
final
Timer
timer
=
new
Timer
();
@Test
public
void
basics
()
throws
Exception
{
TestAccumulator
accumulator
=
new
TestAccumulator
();
assertEquals
(
"incorrect timer"
,
timer
,
accumulator
.
timer
());
assertEquals
(
"incorrect max events"
,
5
,
accumulator
.
maxEvents
());
assertEquals
(
"incorrect max ms"
,
100
,
accumulator
.
maxBatchMillis
());
assertEquals
(
"incorrect idle ms"
,
50
,
accumulator
.
maxIdleMillis
());
}
@Test
public
void
eventTrigger
()
{
TestAccumulator
accumulator
=
new
TestAccumulator
();
accumulator
.
add
(
new
TestEvent
(
FOO
,
"a"
));
accumulator
.
add
(
new
TestEvent
(
FOO
,
"b"
));
accumulator
.
add
(
new
TestEvent
(
FOO
,
"c"
));
accumulator
.
add
(
new
TestEvent
(
FOO
,
"d"
));
assertTrue
(
"should not have fired yet"
,
accumulator
.
batch
.
isEmpty
());
accumulator
.
add
(
new
TestEvent
(
FOO
,
"e"
));
delay
(
10
);
assertFalse
(
"should have fired"
,
accumulator
.
batch
.
isEmpty
());
assertEquals
(
"incorrect batch"
,
"abcde"
,
accumulator
.
batch
);
}
@Test
public
void
timeTrigger
()
{
TestAccumulator
accumulator
=
new
TestAccumulator
();
accumulator
.
add
(
new
TestEvent
(
FOO
,
"a"
));
delay
(
40
);
assertTrue
(
"should not have fired yet"
,
accumulator
.
batch
.
isEmpty
());
accumulator
.
add
(
new
TestEvent
(
FOO
,
"b"
));
delay
(
40
);
assertTrue
(
"should not have fired yet"
,
accumulator
.
batch
.
isEmpty
());
accumulator
.
add
(
new
TestEvent
(
FOO
,
"c"
));
delay
(
40
);
assertFalse
(
"should have fired"
,
accumulator
.
batch
.
isEmpty
());
assertEquals
(
"incorrect batch"
,
"abc"
,
accumulator
.
batch
);
}
@Test
public
void
idleTrigger
()
{
TestAccumulator
accumulator
=
new
TestAccumulator
();
accumulator
.
add
(
new
TestEvent
(
FOO
,
"a"
));
assertTrue
(
"should not have fired yet"
,
accumulator
.
batch
.
isEmpty
());
accumulator
.
add
(
new
TestEvent
(
FOO
,
"b"
));
delay
(
80
);
assertFalse
(
"should have fired"
,
accumulator
.
batch
.
isEmpty
());
assertEquals
(
"incorrect batch"
,
"ab"
,
accumulator
.
batch
);
}
private
class
TestAccumulator
extends
AbstractEventAccumulator
{
String
batch
=
""
;
protected
TestAccumulator
()
{
super
(
timer
,
5
,
100
,
50
);
}
@Override
public
void
processEvents
(
List
<
Event
>
events
)
{
for
(
Event
event
:
events
)
{
batch
+=
event
.
subject
();
}
}
}
}
core/pom.xml
View file @
931af4e
...
...
@@ -26,11 +26,14 @@
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
</dependency>
<dependency>
<groupId>
org.onlab.onos
</groupId>
<artifactId>
onlab-misc
</artifactId>
</dependency>
<dependency>
<groupId>
org.onlab.onos
</groupId>
<artifactId>
onlab-junit
</artifactId>
</dependency>
</dependencies>
<build>
...
...
pom.xml
View file @
931af4e
...
...
@@ -130,6 +130,12 @@
<artifactId>
onlab-misc
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.onlab.onos
</groupId>
<artifactId>
onlab-junit
</artifactId>
<version>
1.0.0-SNAPSHOT
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.onlab.onos
</groupId>
...
...
utils/junit/pom.xml
0 → 100644
View file @
931af4e
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
org.onlab.onos
</groupId>
<artifactId>
onlab-utils
</artifactId>
<version>
1.0.0-SNAPSHOT
</version>
<relativePath>
../pom.xml
</relativePath>
</parent>
<artifactId>
onlab-junit
</artifactId>
<packaging>
bundle
</packaging>
<description>
ON.Lab JUnit test utilities
</description>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava-testlib
</artifactId>
<scope>
compile
</scope>
</dependency>
</dependencies>
</project>
utils/junit/src/main/java/org/onlab/junit/TestTools.java
0 → 100644
View file @
931af4e
package
org
.
onlab
.
junit
;
import
static
com
.
google
.
common
.
base
.
Preconditions
.
checkArgument
;
import
static
org
.
junit
.
Assert
.
fail
;
/**
* Utilities to aid in producing JUnit tests.
*/
public
final
class
TestTools
{
// Prohibit construction
private
TestTools
()
{
}
/**
* Suspends the current thread for a specified number of millis.
*
* @param ms number of millis
*/
public
static
void
delay
(
int
ms
)
{
try
{
Thread
.
sleep
(
ms
);
}
catch
(
InterruptedException
e
)
{
fail
(
"test interrupted"
);
}
}
/**
* Returns the current time in millis since epoch.
*
* @return current time
*/
public
static
long
now
()
{
return
System
.
currentTimeMillis
();
}
/**
* Runs the specified runnable until it completes successfully or until the
* specified time expires. If the latter occurs, the first encountered
* assertion on the last attempt will be re-thrown. Errors other than
* assertion errors will be propagated immediately.
* <p>
* Assertions attempts will not be closer than 10 millis apart and no
* further than 50 millis.
* </p>
*
* @param delay number of millis to delay before the first attempt
* @param duration number of milliseconds beyond the current time
* @param assertions test assertions runnable
*/
public
static
void
assertAfter
(
int
delay
,
int
duration
,
Runnable
assertions
)
{
checkArgument
(
delay
<
duration
,
"delay >= duration"
);
long
start
=
now
();
int
step
=
Math
.
max
(
Math
.
min
((
duration
-
delay
)
/
100
,
50
),
10
);
// Is there an initial delay?
if
(
delay
>
0
)
{
delay
(
delay
);
}
// Keep going until the assertions succeed or until time runs-out.
while
(
true
)
{
try
{
assertions
.
run
();
break
;
}
catch
(
AssertionError
e
)
{
// If there was an error and time ran out, re-throw it.
if
(
now
()
-
start
>
duration
)
{
throw
e
;
}
}
delay
(
step
);
}
}
/**
* Runs the specified runnable until it completes successfully or until the
* specified time expires. If the latter occurs, the first encountered
* assertion on the last attempt will be re-thrown. Errors other than
* assertion errors will be propagated immediately.
* <p>
* Assertions attempts will not be closer than 10 millis apart and no
* further than 50 millis.
* </p>
*
* @param duration number of milliseconds beyond the current time
* @param assertions test assertions runnable
*/
public
static
void
assertAfter
(
int
duration
,
Runnable
assertions
)
{
assertAfter
(
0
,
duration
,
assertions
);
}
}
utils/junit/src/main/javadoc/org/onlab/junit/package.html
0 → 100644
View file @
931af4e
<body>
Utilities to assist in developing JUnit tests.
</body>
\ No newline at end of file
utils/junit/src/test/java/org/onlab/junit/TestToolsTest.java
0 → 100644
View file @
931af4e
package
org
.
onlab
.
junit
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
onlab
.
junit
.
TestTools
.
assertAfter
;
public
class
TestToolsTest
{
@Test
public
void
testSuccess
()
{
assertAfter
(
10
,
100
,
new
Runnable
()
{
int
count
=
0
;
@Override
public
void
run
()
{
if
(
count
++
<
3
)
{
assertTrue
(
false
);
}
}
});
}
@Test
(
expected
=
AssertionError
.
class
)
public
void
testFailure
()
{
assertAfter
(
100
,
new
Runnable
()
{
@Override
public
void
run
()
{
assertTrue
(
false
);
}
});
}
}
utils/pom.xml
View file @
931af4e
...
...
@@ -17,6 +17,7 @@
<description>
Domain agnostic ON.Lab utilities
</description>
<modules>
<module>
junit
</module>
<module>
misc
</module>
<module>
osgi
</module>
<module>
rest
</module>
...
...
Please
register
or
login
to post a comment