Skip to content

Commit

Permalink
Fix copy constructor of EvictionConfig (hazelcast#18813)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetmircik committed Jun 3, 2021
1 parent dedacb6 commit 6cac553
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public EvictionConfig() {
}

public EvictionConfig(EvictionConfig config) {
this.sizeConfigured = true;
this.sizeConfigured = config.sizeConfigured;
this.size = config.size;
this.maxSizePolicy = config.maxSizePolicy;
this.evictionPolicy = config.evictionPolicy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.client.map.impl.nearcache;

import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.NearCacheConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class ClientMapNearCacheEvictionTest extends HazelcastTestSupport {

private final TestHazelcastFactory factory = new TestHazelcastFactory();

@After
public void tearDown() {
factory.terminateAll();
}

@Test
public void near_cache_size_equals_map_size_when_eviction_policy_is_none() {
String mapName = "test";
NearCacheConfig nearCacheConfig = new NearCacheConfig(mapName);
nearCacheConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.NONE);

ClientConfig clientConfig = new ClientConfig();
clientConfig.addNearCacheConfig(nearCacheConfig);

HazelcastInstance server = factory.newHazelcastInstance();
HazelcastInstance client = factory.newHazelcastClient(clientConfig);
IMap<Integer, Integer> map = client.getMap(mapName);

// populate map
int mapSize = 99_999;
for (int i = 0; i < mapSize; i++) {
map.set(i, i);
}

// populate near-cache
for (int i = 0; i < mapSize; i++) {
map.get(i);
}

assertEquals(mapSize, map.getLocalMapStats()
.getNearCacheStats().getOwnedEntryCount());
}

@Test
public void no_more_entries_than_max_near_cache_size_when_eviction_policy_is_not_none() {
String mapName = "mapName";
NearCacheConfig nearCacheConfig = new NearCacheConfig(mapName);
int maxNearCacheSize = 9_999;
nearCacheConfig.getEvictionConfig()
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(maxNearCacheSize);

ClientConfig clientConfig = new ClientConfig();
clientConfig.addNearCacheConfig(nearCacheConfig);

HazelcastInstance server = factory.newHazelcastInstance();
HazelcastInstance client = factory.newHazelcastClient(clientConfig);
IMap<Integer, Integer> map = client.getMap(mapName);

// populate map
int mapSize = 99_999;
for (int i = 0; i < mapSize; i++) {
map.set(i, i);
}

// populate near-cache
for (int i = 0; i < mapSize; i++) {
map.get(i);
}

assertEquals(maxNearCacheSize, map.getLocalMapStats()
.getNearCacheStats().getOwnedEntryCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class EvictionConfigReadOnlyTest {
Expand Down Expand Up @@ -55,4 +57,12 @@ public void setComparatorClassNameOnReadOnlyEvictionConfigShouldFail() {
public void setComparatorOnReadOnlyEvictionConfigShouldFail() {
getReadOnlyConfig().setComparator(null);
}

@Test
public void copy_constructor_copies_right_value_of_field_sizeConfigured() {
EvictionConfig evictionConfig = new EvictionConfig();
EvictionConfig asReadOnly = new EvictionConfigReadOnly(evictionConfig);

assertEquals(evictionConfig.sizeConfigured, asReadOnly.sizeConfigured);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.map.impl.nearcache;

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.NearCacheConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class MapNearCacheEvictionTest extends HazelcastTestSupport {

@Test
public void near_cache_size_equals_map_size_when_eviction_policy_is_none() {
NearCacheConfig nearCacheConfig = new NearCacheConfig();
nearCacheConfig.setCacheLocalEntries(true);
nearCacheConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.NONE);

Config config = getConfig();
String mapName = "mapName";
config.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);

HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, Integer> map = hazelcastInstance.getMap(mapName);

// populate map
int mapSize = 99_999;
for (int i = 0; i < mapSize; i++) {
map.set(i, i);
}

// populate near-cache
for (int i = 0; i < mapSize; i++) {
map.get(i);
}

assertEquals(mapSize, map.getLocalMapStats()
.getNearCacheStats().getOwnedEntryCount());
}

@Test
public void no_more_entries_than_max_near_cache_size_when_eviction_policy_is_not_none() {
NearCacheConfig nearCacheConfig = new NearCacheConfig();
nearCacheConfig.setCacheLocalEntries(true);
int maxNearCacheSize = 9_999;
nearCacheConfig.getEvictionConfig()
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(maxNearCacheSize);

Config config = getConfig();
String mapName = "mapName";
config.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);

HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, Integer> map = hazelcastInstance.getMap(mapName);

// populate map
int mapSize = 99_999;
for (int i = 0; i < mapSize; i++) {
map.set(i, i);
}

// populate near-cache
for (int i = 0; i < mapSize; i++) {
map.get(i);
}

assertEquals(maxNearCacheSize, map.getLocalMapStats()
.getNearCacheStats().getOwnedEntryCount());
}
}

0 comments on commit 6cac553

Please sign in to comment.