|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dubbo.registry.consul; |
| 18 | + |
| 19 | +import com.pszymczyk.consul.ConsulProcess; |
| 20 | +import com.pszymczyk.consul.ConsulStarterBuilder; |
| 21 | +import org.apache.dubbo.common.URL; |
| 22 | +import org.apache.dubbo.common.status.Status; |
| 23 | +import org.apache.dubbo.registry.NotifyListener; |
| 24 | +import org.apache.dubbo.registry.Registry; |
| 25 | +import org.apache.dubbo.registry.status.RegistryStatusChecker; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +import static org.hamcrest.CoreMatchers.is; |
| 36 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 37 | +import static org.hamcrest.CoreMatchers.not; |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | + |
| 41 | +public class ConsulRegistryTest { |
| 42 | + |
| 43 | + private static ConsulProcess consul; |
| 44 | + private ConsulRegistry consulRegistry; |
| 45 | + private String service = "org.apache.dubbo.test.injvmServie"; |
| 46 | + private URL serviceUrl = URL.valueOf("consul://127.0.0.1:8012/" + service + "?notify=false&methods=test1,test2"); |
| 47 | + private URL registryUrl; |
| 48 | + private ConsulRegistryFactory consulRegistryFactory; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setUp() throws Exception { |
| 52 | + this.consul = ConsulStarterBuilder.consulStarter() |
| 53 | + .build() |
| 54 | + .start(); |
| 55 | + this.registryUrl = URL.valueOf("consul://localhost:" + consul.getHttpPort()); |
| 56 | + |
| 57 | + consulRegistryFactory = new ConsulRegistryFactory(); |
| 58 | + this.consulRegistry = (ConsulRegistry) consulRegistryFactory.createRegistry(registryUrl); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + public void tearDown() throws Exception { |
| 63 | + consul.close(); |
| 64 | + this.consulRegistry.destroy(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testRegister() { |
| 69 | + Set<URL> registered; |
| 70 | + |
| 71 | + for (int i = 0; i < 2; i++) { |
| 72 | + consulRegistry.register(serviceUrl); |
| 73 | + registered = consulRegistry.getRegistered(); |
| 74 | + assertThat(registered.contains(serviceUrl), is(true)); |
| 75 | + } |
| 76 | + |
| 77 | + registered = consulRegistry.getRegistered(); |
| 78 | + |
| 79 | + assertThat(registered.size(), is(1)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testSubscribe() { |
| 84 | + NotifyListener listener = mock(NotifyListener.class); |
| 85 | + consulRegistry.subscribe(serviceUrl, listener); |
| 86 | + |
| 87 | + Map<URL, Set<NotifyListener>> subscribed = consulRegistry.getSubscribed(); |
| 88 | + assertThat(subscribed.size(), is(1)); |
| 89 | + assertThat(subscribed.get(serviceUrl).size(), is(1)); |
| 90 | + |
| 91 | + consulRegistry.unsubscribe(serviceUrl, listener); |
| 92 | + subscribed = consulRegistry.getSubscribed(); |
| 93 | + assertThat(subscribed.size(), is(1)); |
| 94 | + assertThat(subscribed.get(serviceUrl).size(), is(0)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testAvailable() { |
| 99 | + consulRegistry.register(serviceUrl); |
| 100 | + assertThat(consulRegistry.isAvailable(), is(true)); |
| 101 | + |
| 102 | +// consulRegistry.destroy(); |
| 103 | +// assertThat(consulRegistry.isAvailable(), is(false)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testLookup() throws InterruptedException { |
| 108 | + List<URL> lookup = consulRegistry.lookup(serviceUrl); |
| 109 | + assertThat(lookup.size(), is(0)); |
| 110 | + |
| 111 | + consulRegistry.register(serviceUrl); |
| 112 | + Thread.sleep(5000); |
| 113 | + lookup = consulRegistry.lookup(serviceUrl); |
| 114 | + assertThat(lookup.size(), is(1)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testStatusChecker() { |
| 119 | + RegistryStatusChecker registryStatusChecker = new RegistryStatusChecker(); |
| 120 | + Status status = registryStatusChecker.check(); |
| 121 | + assertThat(status.getLevel(), is(Status.Level.UNKNOWN)); |
| 122 | + |
| 123 | + Registry registry = consulRegistryFactory.getRegistry(registryUrl); |
| 124 | + assertThat(registry, not(nullValue())); |
| 125 | + |
| 126 | + status = registryStatusChecker.check(); |
| 127 | + assertThat(status.getLevel(), is(Status.Level.OK)); |
| 128 | + |
| 129 | + registry.register(serviceUrl); |
| 130 | + status = registryStatusChecker.check(); |
| 131 | + assertThat(status.getLevel(), is(Status.Level.OK)); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments