-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(proxy): add unit test for ProxyService
Signed-off-by: SSpirits <admin@lv5.moe>
- Loading branch information
1 parent
0a7e33c
commit 8e2541e
Showing
8 changed files
with
203 additions
and
15 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
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
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
104 changes: 104 additions & 0 deletions
104
proxy/src/test/java/com/automq/rocketmq/proxy/grpc/GrpcServerRule.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,104 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 com.automq.rocketmq.proxy.grpc; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.Server; | ||
import io.grpc.inprocess.InProcessChannelBuilder; | ||
import io.grpc.inprocess.InProcessServerBuilder; | ||
import io.grpc.util.MutableHandlerRegistry; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public final class GrpcServerRule implements BeforeEachCallback, AfterEachCallback { | ||
private ManagedChannel channel; | ||
private Server server; | ||
private String serverName; | ||
private MutableHandlerRegistry serviceRegistry; | ||
private boolean useDirectExecutor; | ||
|
||
public GrpcServerRule() { | ||
} | ||
|
||
public GrpcServerRule directExecutor() { | ||
Preconditions.checkState(this.serverName == null, "directExecutor() can only be called at the rule instantiation"); | ||
this.useDirectExecutor = true; | ||
return this; | ||
} | ||
|
||
public ManagedChannel getChannel() { | ||
return this.channel; | ||
} | ||
|
||
public Server getServer() { | ||
return this.server; | ||
} | ||
|
||
public String getServerName() { | ||
return this.serverName; | ||
} | ||
|
||
public MutableHandlerRegistry getServiceRegistry() { | ||
return this.serviceRegistry; | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
this.serverName = null; | ||
this.serviceRegistry = null; | ||
this.channel.shutdown(); | ||
this.server.shutdown(); | ||
|
||
try { | ||
this.channel.awaitTermination(1L, TimeUnit.MINUTES); | ||
this.server.awaitTermination(1L, TimeUnit.MINUTES); | ||
} catch (InterruptedException var5) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(var5); | ||
} finally { | ||
this.channel.shutdownNow(); | ||
this.channel = null; | ||
this.server.shutdownNow(); | ||
this.server = null; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
this.serverName = UUID.randomUUID().toString(); | ||
this.serviceRegistry = new MutableHandlerRegistry(); | ||
InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(this.serverName).fallbackHandlerRegistry(this.serviceRegistry); | ||
if (this.useDirectExecutor) { | ||
serverBuilder.directExecutor(); | ||
} | ||
|
||
this.server = serverBuilder.build().start(); | ||
InProcessChannelBuilder channelBuilder = InProcessChannelBuilder.forName(this.serverName); | ||
if (this.useDirectExecutor) { | ||
channelBuilder.directExecutor(); | ||
} | ||
|
||
this.channel = channelBuilder.build(); | ||
} | ||
} | ||
|
78 changes: 78 additions & 0 deletions
78
proxy/src/test/java/com/automq/rocketmq/proxy/grpc/ProxyServiceImplTest.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,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 com.automq.rocketmq.proxy.grpc; | ||
|
||
import apache.rocketmq.common.v1.Code; | ||
import apache.rocketmq.proxy.v1.ProxyServiceGrpc; | ||
import apache.rocketmq.proxy.v1.Status; | ||
import com.automq.rocketmq.common.config.BrokerConfig; | ||
import com.automq.rocketmq.common.model.FlatMessageExt; | ||
import com.automq.rocketmq.proxy.grpc.client.GrpcProxyClient; | ||
import com.automq.rocketmq.proxy.mock.MockMessageUtil; | ||
import com.automq.rocketmq.proxy.service.ExtendMessageService; | ||
import com.automq.rocketmq.store.api.MessageStore; | ||
import com.automq.rocketmq.store.model.message.PutResult; | ||
import java.lang.reflect.Field; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentMap; | ||
import org.apache.rocketmq.broker.client.ConsumerManager; | ||
import org.apache.rocketmq.broker.client.ProducerManager; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ProxyServiceImplTest { | ||
private static final String TARGET = "target"; | ||
|
||
@RegisterExtension | ||
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); | ||
GrpcProxyClient proxyClient; | ||
|
||
@BeforeEach | ||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public void setup() throws NoSuchFieldException, IllegalAccessException { | ||
MessageStore messageStore = mock(MessageStore.class); | ||
when(messageStore.put(any(), any())).thenReturn(CompletableFuture.completedFuture(new PutResult(PutResult.Status.PUT_OK, 0))); | ||
|
||
ExtendMessageService messageService = mock(ExtendMessageService.class); | ||
ProducerManager producerManager = mock(ProducerManager.class); | ||
ConsumerManager consumerManager = mock(ConsumerManager.class); | ||
ProxyServiceImpl server = new ProxyServiceImpl(messageStore, messageService, producerManager, consumerManager); | ||
grpcServerRule.getServiceRegistry().addService(server); | ||
|
||
ProxyServiceGrpc.ProxyServiceFutureStub stub = ProxyServiceGrpc.newFutureStub(grpcServerRule.getChannel()); | ||
proxyClient = new GrpcProxyClient(new BrokerConfig()); | ||
|
||
Field field = proxyClient.getClass().getDeclaredField("stubMap"); | ||
field.setAccessible(true); | ||
ConcurrentMap<String, ProxyServiceGrpc.ProxyServiceFutureStub> stubMap = (ConcurrentMap) field.get(proxyClient); | ||
stubMap.put(TARGET, stub); | ||
} | ||
|
||
@Test | ||
void relay() { | ||
FlatMessageExt messageExt = MockMessageUtil.buildMessage(0, 1, ""); | ||
Status status = proxyClient.relayMessage(TARGET, messageExt.message()).join(); | ||
assertEquals(Code.OK, status.getCode()); | ||
} | ||
} |
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
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
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