Skip to content

Commit 8668e28

Browse files
committed
test: add test to check NFSClient lease expiration
Acked-by: Lea Morschel Target: master
1 parent 88ecc6d commit 8668e28

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

core/src/main/java/org/dcache/nfs/v4/NFSv4StateHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package org.dcache.nfs.v4;
2121

22+
import com.google.common.annotations.VisibleForTesting;
2223
import com.google.common.util.concurrent.ThreadFactoryBuilder;
2324
import java.time.Clock;
2425
import java.time.Duration;
@@ -106,7 +107,7 @@ public class NFSv4StateHandler {
106107
/**
107108
* Clock to use for all time related operations.
108109
*/
109-
private final Clock _clock = Clock.systemDefaultZone();
110+
private final Clock _clock;
110111

111112
public NFSv4StateHandler() {
112113
this(Duration.ofSeconds(NFSv4Defaults.NFS4_LEASE_TIME), 0, new EphemeralClientRecoveryStore());
@@ -125,8 +126,14 @@ public NFSv4StateHandler(Duration leaseTime, int instanceId, ClientRecoveryStore
125126
}
126127

127128
public NFSv4StateHandler(Duration leaseTime, int instanceId, ClientRecoveryStore clientStore, ClientCache clientsByServerId) {
129+
this(leaseTime, instanceId, clientStore, new DefaultClientCache(leaseTime, new DeadClientCollector(clientStore)), Clock.systemDefaultZone());
130+
}
131+
132+
@VisibleForTesting
133+
NFSv4StateHandler(Duration leaseTime, int instanceId, ClientRecoveryStore clientStore, ClientCache clientsByServerId, Clock clock) {
128134
_leaseTime = leaseTime;
129135
_clientsByServerId = clientsByServerId;
136+
_clock = clock;
130137

131138
_running = true;
132139
_instanceId = instanceId;

core/src/test/java/org/dcache/nfs/v4/NFS4ClientTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009 - 2020 Deutsches Elektronen-Synchroton,
2+
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
33
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
44
*
55
* This library is free software; you can redistribute it and/or modify
@@ -21,11 +21,15 @@
2121

2222
import java.net.UnknownHostException;
2323
import java.nio.charset.StandardCharsets;
24+
import java.time.Duration;
25+
import java.time.temporal.ChronoUnit;
2426
import java.util.concurrent.atomic.AtomicBoolean;
2527

2628
import org.dcache.nfs.ChimeraNFSException;
2729
import org.dcache.nfs.nfsstat;
2830
import org.dcache.nfs.status.SeqMisorderedException;
31+
import org.dcache.nfs.util.ManualClock;
32+
import org.dcache.nfs.util.NopCacheEventListener;
2933
import org.dcache.nfs.v4.xdr.nfs_argop4;
3034
import org.dcache.nfs.v4.xdr.nfs_opnum4;
3135
import org.dcache.nfs.v4.xdr.nfs_resop4;
@@ -49,12 +53,20 @@ public class NFS4ClientTest {
4953
private NFSv4StateHandler stateHandler;
5054
private NFS4Client nfsClient;
5155
private StateOwner owner;
56+
private ManualClock clock;
5257

5358
@Before
5459
public void setUp() throws UnknownHostException, ChimeraNFSException {
55-
stateHandler = new NFSv4StateHandler();
60+
61+
clock = new ManualClock();
62+
var leaseTime = Duration.ofSeconds(NFSv4Defaults.NFS4_LEASE_TIME);
63+
var clientStore = new EphemeralClientRecoveryStore();
64+
stateHandler = new NFSv4StateHandler(leaseTime, 0, clientStore,
65+
new DefaultClientCache(leaseTime, new NopCacheEventListener<>()), clock);
66+
5667
nfsClient = createClient(stateHandler);
57-
owner = nfsClient.getOrCreateOwner("client test".getBytes(StandardCharsets.UTF_8), new seqid4(0));
68+
owner = nfsClient.getOrCreateOwner("client test".getBytes(StandardCharsets.UTF_8),
69+
new seqid4(0));
5870
}
5971

6072
@Test
@@ -171,5 +183,23 @@ public void testClientDisposeCleansState() throws ChimeraNFSException {
171183
assertTrue("client state is not disposed", isDisposed.get());
172184
assertFalse("client claims to have a state after dispose", nfsClient.hasState());
173185
}
186+
187+
@Test
188+
public void testClientValidityBeforeLeaseExpired() throws ChimeraNFSException {
189+
190+
assertTrue(nfsClient.isLeaseValid());
191+
192+
clock.advance(stateHandler.getLeaseTime().minus(1, ChronoUnit.SECONDS));
193+
assertTrue("Client should be valid before lease have expired.", nfsClient.isLeaseValid());
194+
}
195+
196+
@Test
197+
public void testClientValidityAfterLeaseExpired() throws ChimeraNFSException {
198+
199+
assertTrue(nfsClient.isLeaseValid());
200+
201+
clock.advance(stateHandler.getLeaseTime().plus(1, ChronoUnit.SECONDS));
202+
assertFalse("Client can be valid with expired lease", nfsClient.isLeaseValid());
203+
}
174204
}
175205

0 commit comments

Comments
 (0)