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
f8183a4c
Commit
f8183a4c
authored
12 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Refactored DatabaseCleanerImpl to use Timer and TimerTask.
parent
7a0e22d4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
components/net/sf/briar/db/DatabaseCleanerImpl.java
+24
-35
24 additions, 35 deletions
components/net/sf/briar/db/DatabaseCleanerImpl.java
test/net/sf/briar/db/DatabaseCleanerImplTest.java
+28
-6
28 additions, 6 deletions
test/net/sf/briar/db/DatabaseCleanerImplTest.java
with
52 additions
and
41 deletions
components/net/sf/briar/db/DatabaseCleanerImpl.java
+
24
−
35
View file @
f8183a4c
package
net.sf.briar.db
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
net.sf.briar.api.db.DbException
;
class
DatabaseCleanerImpl
implements
DatabaseCleaner
,
Runnable
{
class
DatabaseCleanerImpl
extends
TimerTask
implements
DatabaseCleaner
{
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
DatabaseCleanerImpl
.
class
.
getName
());
Logger
.
getLogger
(
DatabaseCleanerImpl
.
class
.
getName
());
private
Callback
callback
=
null
;
private
long
msBetweenSweeps
=
0L
;
private
boolean
stopped
=
false
;
private
volatile
Callback
callback
=
null
;
private
volatile
Timer
timer
=
null
;
public
synchronized
void
startCleaning
(
Callback
callback
,
long
msBetweenSweeps
)
{
public
void
startCleaning
(
Callback
callback
,
long
msBetweenSweeps
)
{
this
.
callback
=
callback
;
t
his
.
msBetweenSweeps
=
msBetweenSweeps
;
new
Thread
(
this
).
start
(
);
t
imer
=
new
Timer
()
;
timer
.
scheduleAtFixedRate
(
this
,
0L
,
msBetweenSweeps
);
}
public
synchronized
void
stopCleaning
()
{
stopped
=
true
;
notifyAl
l
();
public
void
stopCleaning
()
{
if
(
timer
==
null
)
throw
new
IllegalStateException
()
;
timer
.
cance
l
();
}
public
void
run
()
{
while
(
true
)
{
synchronized
(
this
)
{
if
(
stopped
)
return
;
try
{
if
(
callback
.
shouldCheckFreeSpace
())
{
callback
.
checkFreeSpaceAndClean
();
}
else
{
try
{
wait
(
msBetweenSweeps
);
}
catch
(
InterruptedException
e
)
{
if
(
LOG
.
isLoggable
(
Level
.
INFO
))
LOG
.
info
(
"Interrupted while waiting to clean"
);
Thread
.
currentThread
().
interrupt
();
return
;
}
}
}
catch
(
DbException
e
)
{
if
(
LOG
.
isLoggable
(
Level
.
WARNING
))
LOG
.
warning
(
e
.
toString
());
}
catch
(
RuntimeException
e
)
{
if
(
LOG
.
isLoggable
(
Level
.
WARNING
))
LOG
.
warning
(
e
.
toString
());
}
if
(
callback
==
null
)
throw
new
IllegalStateException
();
try
{
if
(
callback
.
shouldCheckFreeSpace
())
{
callback
.
checkFreeSpaceAndClean
();
}
}
catch
(
DbException
e
)
{
if
(
LOG
.
isLoggable
(
Level
.
WARNING
))
LOG
.
warning
(
e
.
toString
());
throw
new
Error
(
e
);
// Kill the application
}
catch
(
RuntimeException
e
)
{
if
(
LOG
.
isLoggable
(
Level
.
WARNING
))
LOG
.
warning
(
e
.
toString
());
throw
new
Error
(
e
);
// Kill the application
}
}
}
This diff is collapsed.
Click to expand it.
test/net/sf/briar/db/DatabaseCleanerImplTest.java
+
28
−
6
View file @
f8183a4c
...
...
@@ -11,29 +11,51 @@ import org.junit.Test;
public
class
DatabaseCleanerImplTest
extends
BriarTestCase
{
@Test
public
void
testCleanerRunsPeriodically
()
throws
Exception
{
final
CountDownLatch
latch
=
new
CountDownLatch
(
5
);
Callback
callback
=
new
Callback
()
{
public
void
checkFreeSpaceAndClean
()
throws
DbException
{
latch
.
countDown
();
}
public
boolean
shouldCheckFreeSpace
()
{
return
true
;
}
};
DatabaseCleanerImpl
cleaner
=
new
DatabaseCleanerImpl
();
// Start the cleaner
cleaner
.
startCleaning
(
callback
,
10L
);
// The database should be cleaned five times (allow 5s for system load)
assertTrue
(
latch
.
await
(
5
,
TimeUnit
.
SECONDS
));
// Stop the cleaner
cleaner
.
stopCleaning
();
}
@Test
public
void
testStoppingCleanerWakesItUp
()
throws
Exception
{
final
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
Callback
callback
=
new
Callback
()
{
public
void
checkFreeSpaceAndClean
()
throws
DbException
{
throw
new
IllegalStateExceptio
n
();
latch
.
countDow
n
();
}
public
boolean
shouldCheckFreeSpace
()
{
latch
.
countDown
();
return
false
;
return
true
;
}
};
DatabaseCleanerImpl
cleaner
=
new
DatabaseCleanerImpl
();
long
start
=
System
.
currentTimeMillis
();
// Start the cleaner and check that shouldCheckFreeSpace() is called
cleaner
.
startCleaning
(
callback
,
30L
*
1000L
);
// Start the cleaner
cleaner
.
startCleaning
(
callback
,
10L
*
1000L
);
// The database should be cleaned once at startup
assertTrue
(
latch
.
await
(
5
,
TimeUnit
.
SECONDS
));
// Stop the cleaner (it should be waiting between sweeps)
cleaner
.
stopCleaning
();
long
end
=
System
.
currentTimeMillis
();
// Check that much less than
3
0 seconds expired
// Check that much less than
1
0 seconds expired
assertTrue
(
end
-
start
<
10L
*
1000L
);
}
}
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