Skip to content

Commit

Permalink
add test case. (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
tydhot authored Jul 16, 2021
1 parent ca66b68 commit ed446ba
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ subprojects {
]

List junit = [
"junit:junit:4.12"
"junit:junit:4.12",
"com.github.stefanbirkner:system-rules:1.16.1"
]

List log = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 org.apache.eventmesh.common;

import org.junit.Assert;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class IPUtilTest {

@Test
public void testDockerIP() {
EnvironmentVariables environmentVariables = new EnvironmentVariables();
environmentVariables.set("docker_host_ip", "dockHostIP");
Assert.assertEquals("dockHostIP", IPUtil.getLocalAddress());
}

@Test
public void testLocalhostIP() {
Assert.assertNotNull(IPUtil.getLocalAddress());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 org.apache.eventmesh.common;

import org.junit.Assert;
import org.junit.Test;

public class ThreadUtilTest {

@Test
public void testRandomSleep() {
TestThread testThread = new TestThread();
testThread.start();
try {
Thread.sleep(100L);
Assert.assertTrue(testThread.getSleepTime() >= 1);
Assert.assertTrue(testThread.getSleepTime() <= 50);
} catch (InterruptedException ignore) {
}
}

@Test
public void testPID() {
Assert.assertNotEquals(-1, ThreadUtil.getPID());
}

class TestThread extends Thread {

private long sleepTime;

public long getSleepTime() {
return sleepTime;
}

public void run() {
long startTime = System.currentTimeMillis();
try {
ThreadUtil.randomSleep(50);
} catch (Exception ignore) {
}
sleepTime = System.currentTimeMillis() - startTime;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 org.apache.eventmesh.common.protocol.tcp.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import org.apache.eventmesh.common.protocol.tcp.Package;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;

public class CodecTest {

@Test
public void testCodec() throws Exception {
Package testP = new Package();
Codec.Encoder ce = new Codec.Encoder();
ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
ce.encode(null, testP, buf);
Codec.Decoder cd = new Codec.Decoder();
ArrayList<Object> result = new ArrayList<>();
cd.decode(null, buf, result);
Assert.assertNotNull(result.get(0));
Assert.assertEquals(result.get(0).toString(), testP.toString());
}

}

0 comments on commit ed446ba

Please sign in to comment.