PageRenderTime 48ms CodeModel.GetById 35ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/bootstrap/src/test/java/io/airlift/bootstrap/TestLifeCycleManager.java

https://gitlab.com/vectorci/airlift
Java | 328 lines | 274 code | 37 blank | 17 comment | 0 complexity | 9ab882812d86115f6efa1fba20d7ad45 MD5 | raw file
  1/*
  2 * Copyright 2010 Proofpoint, Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of 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,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package io.airlift.bootstrap;
 17
 18import com.google.common.collect.Sets;
 19import com.google.inject.Binder;
 20import com.google.inject.CreationException;
 21import com.google.inject.Guice;
 22import com.google.inject.Injector;
 23import com.google.inject.Module;
 24import com.google.inject.PrivateModule;
 25import com.google.inject.Scopes;
 26import com.google.inject.Stage;
 27import org.testng.Assert;
 28import org.testng.annotations.BeforeMethod;
 29import org.testng.annotations.Test;
 30
 31import java.util.Arrays;
 32import java.util.List;
 33import java.util.Set;
 34import java.util.concurrent.CopyOnWriteArrayList;
 35
 36public class TestLifeCycleManager
 37{
 38    private final static List<String> stateLog = new CopyOnWriteArrayList<String>();
 39
 40    @BeforeMethod
 41    public void setup()
 42    {
 43        stateLog.clear();
 44    }
 45
 46    public static void note(String str)
 47    {
 48        // I'm assuming that tests are run serially
 49        stateLog.add(str);
 50    }
 51
 52    @Test
 53    public void testImmediateStarts()
 54            throws Exception
 55    {
 56        Module module = new Module()
 57        {
 58            @Override
 59            public void configure(Binder binder)
 60            {
 61                binder.bind(InstanceThatRequiresStart.class).in(Scopes.SINGLETON);
 62                binder.bind(InstanceThatUsesInstanceThatRequiresStart.class).in(Scopes.SINGLETON);
 63            }
 64        };
 65
 66        Injector injector = Guice.createInjector(
 67                Stage.PRODUCTION,
 68                new LifeCycleModule(),
 69                module
 70        );
 71
 72        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
 73        lifeCycleManager.start();
 74
 75        Assert.assertEquals(stateLog, Arrays.asList("InstanceThatUsesInstanceThatRequiresStart:OK"));
 76    }
 77
 78    @Test
 79    public void testPrivateModule()
 80            throws Exception
 81    {
 82        Module module = new Module()
 83        {
 84            @Override
 85            public void configure(Binder binder)
 86            {
 87                final PrivateModule privateModule = new PrivateModule()
 88                {
 89                    @Override
 90                    protected void configure()
 91                    {
 92                        binder().bind(SimpleBase.class).to(SimpleBaseImpl.class).in(Scopes.SINGLETON);
 93                        binder().expose(SimpleBase.class);
 94                    }
 95                };
 96                binder.install(privateModule);
 97            }
 98        };
 99
100        Injector injector = Guice.createInjector(
101                Stage.PRODUCTION,
102                new LifeCycleModule(),
103                module
104        );
105        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
106        lifeCycleManager.start();
107        Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl"));
108
109        lifeCycleManager.stop();
110        Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl", "preSimpleBaseImpl"));
111    }
112
113    @Test
114    public void testSubClassAnnotated()
115            throws Exception
116    {
117        Module module = new Module()
118        {
119            @Override
120            public void configure(Binder binder)
121            {
122                binder.bind(SimpleBase.class).to(SimpleBaseImpl.class).in(Scopes.SINGLETON);
123            }
124        };
125        Injector injector = Guice.createInjector(
126                Stage.PRODUCTION,
127                new LifeCycleModule(),
128                module
129        );
130        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
131        lifeCycleManager.start();
132        Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl"));
133
134        lifeCycleManager.stop();
135
136        Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl", "preSimpleBaseImpl"));
137    }
138
139    @Test
140    public void testExecuted()
141            throws Exception
142    {
143        Injector injector = Guice.createInjector(
144                Stage.PRODUCTION,
145                new LifeCycleModule(),
146                new Module()
147                {
148                    @Override
149                    public void configure(Binder binder)
150                    {
151                        binder.bind(ExecutedInstance.class).in(Scopes.SINGLETON);
152                    }
153                }
154        );
155        ExecutedInstance instance = injector.getInstance(ExecutedInstance.class);
156
157        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
158        lifeCycleManager.start();
159        instance.waitForStart();
160        Assert.assertEquals(stateLog, Arrays.asList("Starting"));
161
162        lifeCycleManager.stop();
163        instance.waitForEnd();
164
165        Assert.assertEquals(stateLog, Arrays.asList("Starting", "Done"));
166    }
167
168    @Test
169    public void testDeepDependency()
170            throws Exception
171    {
172        Module module = new Module()
173        {
174            @Override
175            public void configure(Binder binder)
176            {
177                binder.bind(AnInstance.class).in(Scopes.SINGLETON);
178                binder.bind(AnotherInstance.class).in(Scopes.SINGLETON);
179                binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
180            }
181        };
182        Injector injector = Guice.createInjector(
183                Stage.PRODUCTION,
184                new LifeCycleModule(),
185                module
186        );
187
188        injector.getInstance(AnotherInstance.class);
189
190        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
191        lifeCycleManager.start();
192        Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance"));
193
194        lifeCycleManager.stop();
195        Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance", "preDependentInstance"));
196    }
197
198    @Test
199    public void testIllegalMethods()
200            throws Exception
201    {
202        try {
203            Guice.createInjector(
204                    Stage.PRODUCTION,
205                    new Module()
206                    {
207                        @Override
208                        public void configure(Binder binder)
209                        {
210                            binder.bind(IllegalInstance.class).in(Scopes.SINGLETON);
211                        }
212                    },
213                    new LifeCycleModule()
214            );
215            Assert.fail();
216        }
217        catch (CreationException dummy) {
218            // correct behavior
219        }
220    }
221
222    @Test
223    public void testDuplicateMethodNames()
224            throws Exception
225    {
226        Injector injector = Guice.createInjector(
227                Stage.PRODUCTION,
228                new Module()
229                {
230                    @Override
231                    public void configure(Binder binder)
232                    {
233                        binder.bind(FooTestInstance.class).in(Scopes.SINGLETON);
234                    }
235                },
236                new LifeCycleModule()
237        );
238
239        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
240        lifeCycleManager.start();
241        lifeCycleManager.stop();
242
243        Assert.assertEquals(stateLog, Arrays.asList("foo"));
244    }
245
246    @Test
247    public void testJITInjection()
248            throws Exception
249    {
250        Injector injector = Guice.createInjector(
251                Stage.PRODUCTION,
252                new LifeCycleModule(),
253                new Module()
254                {
255                    @Override
256                    public void configure(Binder binder)
257                    {
258                        binder.bind(AnInstance.class).in(Scopes.SINGLETON);
259                        binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
260                    }
261                }
262        );
263        injector.getInstance(AnInstance.class);
264
265        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
266        lifeCycleManager.start();
267        lifeCycleManager.stop();
268
269        Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance", "preDependentInstance"));
270    }
271
272    @Test
273    public void testNoPreDestroy()
274            throws Exception
275    {
276        Injector injector = Guice.createInjector(
277                Stage.PRODUCTION,
278                new LifeCycleModule(),
279                new Module()
280                {
281                    @Override
282                    public void configure(Binder binder)
283                    {
284                        binder.bind(PostConstructOnly.class).in(Scopes.SINGLETON);
285                        binder.bind(PreDestroyOnly.class).in(Scopes.SINGLETON);
286                    }
287                }
288        );
289        injector.getInstance(PostConstructOnly.class);
290
291        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
292        lifeCycleManager.start();
293        Assert.assertEquals(stateLog, Arrays.asList("makeMe"));
294
295        lifeCycleManager.stop();
296        Assert.assertEquals(stateLog, Arrays.asList("makeMe", "unmakeMe"));
297    }
298
299    @Test
300    public void testModule()
301            throws Exception
302    {
303        Module module = new Module()
304        {
305            @Override
306            public void configure(Binder binder)
307            {
308                binder.bind(DependentBoundInstance.class).to(DependentInstanceImpl.class).in(Scopes.SINGLETON);
309
310                binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
311                binder.bind(InstanceOne.class).in(Scopes.SINGLETON);
312                binder.bind(InstanceTwo.class).in(Scopes.SINGLETON);
313            }
314        };
315        Injector injector = Guice.createInjector(
316                Stage.PRODUCTION,
317                new LifeCycleModule(),
318                module
319        );
320
321        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
322        lifeCycleManager.start();
323        lifeCycleManager.stop();
324
325        Set<String> stateLogSet = Sets.newHashSet(stateLog);
326        Assert.assertEquals(stateLogSet, Sets.newHashSet("postDependentBoundInstance", "postDependentInstance", "postMakeOne", "postMakeTwo", "preDestroyTwo", "preDestroyOne", "preDependentInstance", "preDependentBoundInstance"));
327    }
328}