Prepare to release 1.7.2
[voldemort/jeffpc.git] / test / unit / voldemort / client / LazyStoreClientTest.java
blob2baec6be10ef10dc5358e40e987c62f1ff6ece02
1 /*
2 * Copyright 2008-2011 LinkedIn, Inc
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
17 package voldemort.client;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
23 import java.util.concurrent.Callable;
25 import org.junit.Before;
26 import org.junit.Test;
28 import voldemort.serialization.Serializer;
29 import voldemort.serialization.StringSerializer;
30 import voldemort.utils.SystemTime;
32 /**
34 public class LazyStoreClientTest extends DefaultStoreClientTest {
36 private MockStoreClientFactory factory;
38 @Override
39 @Before
40 public void setUp() {
41 this.nodeId = 0;
42 this.time = SystemTime.INSTANCE;
43 Serializer<String> serializer = new StringSerializer();
44 this.factory = new MockStoreClientFactory(serializer,
45 serializer,
46 null,
47 serializer,
48 nodeId,
49 time);
50 this.client = newLazyStoreClient(factory);
53 @Test
54 public void testInitializationShouldBeLazy() {
55 StoreClientFactory spyFactory = spy(factory);
56 LazyStoreClient<String, String> spyLazyClient = spy(newLazyStoreClient(spyFactory));
58 // Check that we don't initialize upon construction
59 verify(spyFactory, times(0)).getStoreClient("test");
61 // Check that we initialize once and only once
62 for(int i = 0; i < 10; i++)
63 spyLazyClient.get("test");
65 verify(spyFactory, times(1)).getStoreClient("test");
66 verify(spyLazyClient, times(1)).initStoreClient();
69 private LazyStoreClient<String, String> newLazyStoreClient(final StoreClientFactory factory) {
70 return new LazyStoreClient<String, String>(new Callable<StoreClient<String, String>>() {
72 public StoreClient<String, String> call() throws Exception {
73 return factory.getStoreClient("test");
75 }, false);