-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new CacheClient builder. This is the safest default Netty by default will keep thread local Pools if the application uses several threads in order to save values on the case but it does not perform frequent operations (ByteBuffer allocations) pooling ByteBuffers will lead to a large usage of direct memory which won't be reclaimed, because by default Netty reclaims memory per thread and every N allocations.
- Loading branch information
Enrico Olivelli - Diennea
committed
Mar 22, 2019
1 parent
db9ef94
commit cbf5c3a
Showing
2 changed files
with
226 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
blazingcache-core/src/test/java/blazingcache/client/CacheClientBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
Licensed to Diennea S.r.l. under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. Diennea S.r.l. licenses this file | ||
to you 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 blazingcache.client; | ||
|
||
import blazingcache.network.Channel; | ||
import blazingcache.network.ChannelEventListener; | ||
import blazingcache.network.ConnectionRequestInfo; | ||
import blazingcache.network.ServerLocator; | ||
import blazingcache.network.ServerNotAvailableException; | ||
import blazingcache.network.ServerRejectedConnectionException; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.buffer.UnpooledByteBufAllocator; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests around cache client builder | ||
* | ||
* @author enrico.olivelli | ||
*/ | ||
public class CacheClientBuilderTest { | ||
|
||
private final ServerLocator serverLocator = new ServerLocator() { | ||
@Override | ||
public Channel connect(ChannelEventListener messageReceiver, ConnectionRequestInfo workerInfo) throws InterruptedException, ServerNotAvailableException, ServerRejectedConnectionException { | ||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | ||
} | ||
|
||
@Override | ||
public void brokerDisconnected() { | ||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | ||
} | ||
}; | ||
|
||
@Test | ||
public void testDefault() { | ||
try (CacheClient client = CacheClient.newBuilder() | ||
.serverLocator(serverLocator) | ||
.build();) { | ||
assertTrue(client.isOffHeap()); | ||
assertTrue(client.getAllocator() instanceof UnpooledByteBufAllocator); | ||
assertTrue(client.getClientId().startsWith("localhost")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDefaultLegacyConstructor() { | ||
try (CacheClient client = new CacheClient("ff", "bar", serverLocator)) { | ||
assertTrue(client.isOffHeap()); | ||
assertTrue(client.getAllocator() instanceof UnpooledByteBufAllocator); | ||
assertTrue(client.getClientId().startsWith("ff")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOffHeap() { | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.offHeap(false) | ||
.build();) { | ||
assertFalse(client.isOffHeap()); | ||
} | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.offHeap(true) | ||
.build();) { | ||
assertTrue(client.isOffHeap()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testUsePooledByteBuffers() { | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.poolMemoryBuffers(true) | ||
.build();) { | ||
assertTrue(client.getAllocator() instanceof PooledByteBufAllocator); | ||
} | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.poolMemoryBuffers(false) | ||
.build();) { | ||
assertTrue(client.getAllocator() instanceof UnpooledByteBufAllocator); | ||
} | ||
} | ||
|
||
@Test | ||
public void testClientId() { | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.clientId("foo") | ||
.build();) { | ||
assertTrue(client.getClientId().startsWith("foo")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSharedSecret() { | ||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.sharedSecret("aaa") | ||
.build();) { | ||
assertEquals("aaa", client.getSharedSecret()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testServerLocator() { | ||
|
||
try (CacheClient client = CacheClient | ||
.newBuilder() | ||
.serverLocator(serverLocator) | ||
.build();) { | ||
assertSame(serverLocator, client.getBrokerLocator()); | ||
} | ||
} | ||
|
||
} |