Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Test for ModelRequestEncoder #2580

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.pytorch.serve.util.codec;

import static org.testng.Assert.assertEquals;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import java.io.IOException;
import java.util.ArrayList;
import org.pytorch.serve.util.messages.InputParameter;
import org.pytorch.serve.util.messages.ModelInferenceRequest;
import org.pytorch.serve.util.messages.RequestInput;
import org.testng.annotations.Test;

public class ModelRequestEncoderTest {

@Test
public void testEmpty() {
ChannelHandler encoder = new ModelRequestEncoder(false);
EmbeddedChannel channel = new EmbeddedChannel(encoder);
ModelInferenceRequest msg = new ModelInferenceRequest("testModel");
writeToChannelAndFlush(channel, msg);
byte[] expected =
new byte[] {
'I', (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
};
assertOutboundEquals(channel, expected);
}

@Test
public void testSimple() throws IOException {
ChannelHandler encoder = new ModelRequestEncoder(true);
EmbeddedChannel channel = new EmbeddedChannel(encoder);
ModelInferenceRequest msg = new ModelInferenceRequest("testModel");
ArrayList list = new ArrayList<RequestInput>();
RequestInput input = new RequestInput("request_id");
byte[] compressed =
new byte[] {
'{', '\"', 'd', 'a', 't', 'a', '\"', ':', ' ', '\"', 'v', 'a', 'l', 'u', 'e',
'\"', '}',
};

input.addParameter(new InputParameter("input_name", compressed, "application/json"));
list.add(input);
msg.setRequestBatch(list);
writeToChannelAndFlush(channel, msg);

byte[] expected =
new byte[] {
'I',
0x00,
0x00,
0x00,
0x0A,
'r',
'e',
'q',
'u',
'e',
's',
't',
'_',
'i',
'd',
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF, // end of headers
0x00,
0x00,
0x00,
0x0A,
'i',
'n',
'p',
'u',
't',
'_',
'n',
'a',
'm',
'e',
0x00,
0x00,
0x00,
0x10,
'a',
'p',
'p',
'l',
'i',
'c',
'a',
't',
'i',
'o',
'n',
'/',
'j',
's',
'o',
'n',
0x00,
0x00,
0x00,
(byte) 0x11,
'{',
'\"',
'd',
'a',
't',
'a',
'\"',
':',
' ',
'\"',
'v',
'a',
'l',
'u',
'e',
'\"',
'}',
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF, // end of parameters
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF // end of batch
};
assertOutboundEquals(channel, expected);
}

private void assertOutboundEquals(EmbeddedChannel channel, byte[] expected) {
ByteBuf buf = channel.readOutbound();
byte[] actual = new byte[expected.length];
try {
buf.readBytes(actual, 0, expected.length);
} finally {
buf.release();
}
assertEquals(actual, expected);
}

private void writeToChannelAndFlush(EmbeddedChannel channel, ModelInferenceRequest msg) {
ChannelFuture write = channel.writeAndFlush(msg);
while (true) {
try {
write.sync();
break;
} catch (InterruptedException e) {
}
}
}
}
1 change: 1 addition & 0 deletions frontend/server/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<suite name="TorchServeSuite" verbose="1" >
<test name="TorchServe">
<classes>
<class name="org.pytorch.serve.util.codec.ModelRequestEncoderTest"/>
<class name="org.pytorch.serve.util.ConfigManagerTest"/>
<class name="org.pytorch.serve.util.ConnectorTest"/>
<class name="org.pytorch.serve.util.PluginSdkTest"/>
Expand Down
Loading