Skip to content

Commit

Permalink
util: ensure that cache entry lifetime is bigger than idle time
Browse files Browse the repository at this point in the history
Motivation:
'document' the behavior, avoid miss configurations

Modification:
Add precondition into Cache constructor

Result:
Invalid cache objects can be constructed

Target: master
Acked-by: Albert Rossi
Acked-by: Lea Morschel
  • Loading branch information
kofemann committed Mar 21, 2022
1 parent a045ca7 commit c053eb3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/main/java/org/dcache/nfs/util/Cache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2020 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -32,6 +32,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkArgument;

/**
* A Dictionary where value associated with the key may become unavailable due
* to validity timeout.
Expand Down Expand Up @@ -146,6 +148,9 @@ public Cache(final String name, int size, long entryLifeTime, long entryIdleTime
*/
public Cache(final String name, int size, long entryLifeTime, long entryIdleTime,
CacheEventListener<K, V> eventListener, Clock clock) {

checkArgument(entryLifeTime >= entryIdleTime, "Entry life time cant be smaller that idle time");

_name = name;
_size = size;
_defaultEntryMaxLifeTime = entryLifeTime;
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/java/org/dcache/nfs/util/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public void setUp() {
new NopCacheEventListener(), _clock);
}

@Test(expected = IllegalArgumentException.class)
public void testCacheIdleBiggerThanMax() {
new Cache<>("test cache", 10,
5, 7,
new NopCacheEventListener(), _clock);
}

@Test
public void testPutGet() {

Expand Down

0 comments on commit c053eb3

Please sign in to comment.