Re: Partial restructuring of Freenet's code
Martin Byrenheid <[email protected]> Fri, 26 Apr 2019 15:43:49 +0200
| Newsgroups | gmane.network.freenet.devel |
|---|---|
| Message-ID | <[email protected]> |
On 4/26/19 1:33 PM, Steve Dougherty wrote:
> Yes, mocking time will be helpful. It=E2=80=99s not =C2=A0clear to me h=
ow many
> places that will be of use, or whether mocking would still be viable
> in integration tests instead of just unit tests, but it=E2=80=99s defin=
itely a
> good tool to have. Do you know if JUnit has anything that can help
> with that? It seems like a common enough need that it might.
I just checked if there is anything included in JUnit but it doesn't
seem so. According to answers on stackoverflow, Java 8 provides an
abstract class Clock that can be used instead of creating a custom
interface [1]. To get an instance of the Clock class, one could either
add a corresponding constructor parameter to the class that uses it or
add a method getClock() to the Node class/interface.
Alternatively, there is a framework called PowerMock [2] that can be
used to instrument calls to System.currentTimeMillis() directly[3].
PowerMock still seems to be actively maintained (last commit was 5 days
ago) but I don't know for how long. So, using the Clock class seems to
be the more reliable solution to me.
[1]
https://stackoverflow.com/questions/2001671/override-java-system-currentt=
imemillis-for-testing-time-sensitive-code#answer-26570951
[2] https://github.com/powermock/powermock
[3]
https://stackoverflow.com/questions/28219698/powermock-with-final-class-s=
ystem-and-static-method-currenttimemillis
> Testing concurrent things may require more Conditions for threads to
> wait on. It=E2=80=99s not clear to me how much effort that will require=
to be
> viable, or how best to expose them.
I also don't yet have a good feeling about the effort. I will pick a
class like LocationManager and try to write some test cases for it. =C2=A0=
--=20
Martin Byrenheid
Scientific Assistant
Technische Universit=C3=A4t Dresden
Faculty of Computer Science
Institute of System Architecture
Chair of Privacy and Data Security
01062 Dresden
Tel.: +49 351 463 38235
GPG : F144 FBCD F0C2 257A 87ED=20
CFDE 24EB E684 D0BE E785