2 * Copyright 2008-2011 LinkedIn, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
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
;
34 public class LazyStoreClientTest
extends DefaultStoreClientTest
{
36 private MockStoreClientFactory factory
;
42 this.time
= SystemTime
.INSTANCE
;
43 Serializer
<String
> serializer
= new StringSerializer();
44 this.factory
= new MockStoreClientFactory(serializer
,
50 this.client
= newLazyStoreClient(factory
);
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");