Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
468db2a9
Commit
468db2a9
authored
11 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Use a mock timer to test the database cleaner.
parent
44bb9134
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
briar-tests/src/org/briarproject/db/DatabaseCleanerImplTest.java
+24
-42
24 additions, 42 deletions
...ests/src/org/briarproject/db/DatabaseCleanerImplTest.java
with
24 additions
and
42 deletions
briar-tests/src/org/briarproject/db/DatabaseCleanerImplTest.java
+
24
−
42
View file @
468db2a9
package
org.briarproject.db
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
SECONDS
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.briarproject.BriarTestCase
;
import
org.briarproject.api.db.DbException
;
import
org.briarproject.api.system.Timer
;
import
org.briarproject.db.DatabaseCleaner.Callback
;
import
org.
briarproject.system.SystemTimer
;
import
org.
jmock.Expectations
;
import
org.jmock.Mockery
;
import
org.junit.Test
;
// FIXME: Use a mock timer
public
class
DatabaseCleanerImplTest
extends
BriarTestCase
{
@Test
public
void
testCleanerRunsPeriodically
()
throws
Exception
{
final
CountDownLatch
latch
=
new
CountDownLatch
(
5
);
final
AtomicInteger
cleans
=
new
AtomicInteger
(
0
);
Callback
callback
=
new
Callback
()
{
public
void
checkFreeSpaceAndClean
()
throws
DbException
{
latch
.
countDown
();
}
public
boolean
shouldCheckFreeSpace
()
{
return
true
;
}
};
Timer
timer
=
new
SystemTimer
();
DatabaseCleanerImpl
cleaner
=
new
DatabaseCleanerImpl
(
timer
);
// Start the cleaner
cleaner
.
startCleaning
(
callback
,
10
);
// The database should be cleaned five times (allow 5s for system load)
assertTrue
(
latch
.
await
(
5
,
SECONDS
));
// Stop the cleaner
cleaner
.
stopCleaning
();
}
@Test
public
void
testStoppingCleanerWakesItUp
()
throws
Exception
{
final
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
Callback
callback
=
new
Callback
()
{
boolean
check
=
true
;
public
void
checkFreeSpaceAndClean
()
throws
DbException
{
latch
.
countDown
();
cleans
.
incrementAndGet
();
}
public
boolean
shouldCheckFreeSpace
()
{
return
true
;
// Alternate between true and false
check
=
!
check
;
return
!
check
;
}
};
Timer
timer
=
new
SystemTimer
();
DatabaseCleanerImpl
cleaner
=
new
DatabaseCleanerImpl
(
timer
);
long
start
=
System
.
currentTimeMillis
();
// Start the cleaner
cleaner
.
startCleaning
(
callback
,
10
*
1000
);
// The database should be cleaned once at startup
assertTrue
(
latch
.
await
(
5
,
SECONDS
));
// Stop the cleaner (it should be waiting between sweeps)
Mockery
context
=
new
Mockery
();
final
Timer
timer
=
context
.
mock
(
Timer
.
class
);
final
DatabaseCleanerImpl
cleaner
=
new
DatabaseCleanerImpl
(
timer
);
context
.
checking
(
new
Expectations
()
{{
oneOf
(
timer
).
scheduleAtFixedRate
(
cleaner
,
0
,
10
);
oneOf
(
timer
).
cancel
();
}});
// Start the cleaner - it should schedule itself with the timer
cleaner
.
startCleaning
(
callback
,
10
);
// Call the cleaner's run method six times
for
(
int
i
=
0
;
i
<
6
;
i
++)
cleaner
.
run
();
// Stop the cleaner - it should cancel the timer
cleaner
.
stopCleaning
();
long
end
=
System
.
currentTimeMillis
();
// Check that much less than 10 seconds expired
assertTrue
(
end
-
start
<
10
*
1000
);
// The database should have been cleaned three times
assertEquals
(
3
,
cleans
.
get
());
context
.
assertIsSatisfied
(
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment