Skip to content

Commit

Permalink
add ut for context copy #8
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Oct 15, 2013
1 parent a183aa2 commit 828e77e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/test/java/com/alibaba/mtc/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public String call() {
context.set("key", value);
context.set("p", context.get("p") + value);

if (null != context.get("foo")) {
FooContext foo = context.get("foo");
foo.setName("child");
foo.setAge(100);
}

copiedContent = new HashMap<String, Object>(context.get());

return "ok";
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/com/alibaba/mtc/FooContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alibaba.mtc;

/**
* @author ding.lid
*/
public class FooContext implements Copyable<FooContext> {
String name;
int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public FooContext() {
}

public FooContext(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public FooContext copy() {
return new FooContext(name, age);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

FooContext that = (FooContext) o;

if (age != that.age) return false;
if (name != null ? !name.equals(that.name) : that.name != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
23 changes: 21 additions & 2 deletions src/test/java/com/alibaba/mtc/MtContextCallableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;


Expand Down Expand Up @@ -66,8 +67,6 @@ public void test_MtContextCallable_withExecutorService() throws Exception {
MtContextCallable mtContextCallable = MtContextCallable.get(call);
assertEquals(call, mtContextCallable.getCallable());
Future future = executorService.submit(mtContextCallable);

Thread.sleep(100);
assertEquals("ok", future.get());

// Child independent & Inheritable
Expand All @@ -85,6 +84,26 @@ public void test_MtContextCallable_withExecutorService() throws Exception {
assertEquals("p0", MtContext.getContext().get("p"));
}

@Test
public void test_MtContextCallable_copyObject() throws Exception {
MtContext.getContext().set(new HashMap<String, Object>());
MtContext.getContext().set("parent", "parent");
MtContext.getContext().set("p", "p0");
MtContext.getContext().set("foo", new FooContext("parent", 0));

Call call = new Call("1");
MtContextCallable mtContextCallable = MtContextCallable.get(call);
assertEquals(call, mtContextCallable.getCallable());
executorService.submit(mtContextCallable);

Future future = executorService.submit(mtContextCallable);
assertEquals("ok", future.get());

assertNotSame(call.copiedContent.get("foo"), MtContext.getContext().get("foo"));
assertEquals(new FooContext("child", 100), call.copiedContent.get("foo"));
assertEquals(new FooContext("parent", 0), MtContext.getContext().get("foo"));
}

@Test
public void test_idempotent() throws Exception {
MtContextCallable<String> call = MtContextCallable.get(new Call("1"));
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/alibaba/mtc/MtContextRunnableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.Executors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;


Expand Down Expand Up @@ -82,6 +83,25 @@ public void test_MtContextRunnable_withExecutorService() throws Exception {
assertEquals("p0", MtContext.getContext().get("p"));
}

@Test
public void test_MtContextRunnable_copyObject() throws Exception {
MtContext.getContext().set(new HashMap<String, Object>());
MtContext.getContext().set("parent", "parent");
MtContext.getContext().set("p", "p0");
MtContext.getContext().set("foo", new FooContext("parent", 0));

Task task = new Task("1");
MtContextRunnable mtContextRunnable = MtContextRunnable.get(task);
assertEquals(task, mtContextRunnable.getRunnable());
executorService.execute(mtContextRunnable);

Thread.sleep(100);

assertNotSame(task.copiedContent.get("foo"), MtContext.getContext().get("foo"));
assertEquals(new FooContext("child", 100), task.copiedContent.get("foo"));
assertEquals(new FooContext("parent", 0), MtContext.getContext().get("foo"));
}

@Test
public void test_idempotent() throws Exception {
MtContextRunnable task = MtContextRunnable.get(new Task("1"));
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/alibaba/mtc/MtContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ public void test_thread_independent() throws Exception {
assertEquals("parent", MtContext.getContext().get("parent"));
assertEquals("p0", MtContext.getContext().get("p"));
}

@Test
public void test_thread_copyObject() throws Exception {
MtContext.getContext().set(new HashMap<String, Object>());
MtContext.getContext().set("parent", "parent");
MtContext.getContext().set("p", "p0");
MtContext.getContext().set("foo", new FooContext("parent", 0));

Task task1 = new Task("1");
Thread thread1 = new Thread(task1);

thread1.start();
thread1.join();

assertNotSame(task1.copiedContent.get("foo"), MtContext.getContext().get("foo"));
assertEquals(new FooContext("child", 100), task1.copiedContent.get("foo"));
assertEquals(new FooContext("parent", 0), MtContext.getContext().get("foo"));
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/alibaba/mtc/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public void run() {
context.set("p", context.get("p") + value);
System.out.println("Task " + value + " running2: " + context.get());

if (null != context.get("foo")) {
FooContext foo = context.get("foo");
foo.setName("child");
foo.setAge(100);
}

copiedContent = new HashMap<String, Object>(context.get());

System.out.println("Task " + value + " finished!");
Expand Down

0 comments on commit 828e77e

Please sign in to comment.