CoroutineTest.java 5.5 KB
Newer Older
Y
yunyao.zxl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
 * @summary unit tests for coroutines
 * @run junit/othervm -XX:+EnableCoroutine test.java.dyn.CoroutineTest
 */

package test.java.dyn;

import java.dyn.Coroutine;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class CoroutineTest {
	private StringBuilder seq;

	@Before
	public void before() {
		seq = new StringBuilder();
		seq.append("a");
	}

	@Test
	public void symSequence() {
Y
yunyao.zxl 已提交
53
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
Y
yunyao.zxl 已提交
54 55 56 57
		Coroutine coro = new Coroutine() {
			protected void run() {
				seq.append("c");
				for (int i = 0; i < 3; i++) {
Y
yunyao.zxl 已提交
58
					Coroutine.yieldTo(threadCoro);
Y
yunyao.zxl 已提交
59 60 61 62 63 64
					seq.append("e");
				}
			}
		};
		seq.append("b");
		assertFalse(coro.isFinished());
Y
yunyao.zxl 已提交
65
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
66 67 68
		for (int i = 0; i < 3; i++) {
			seq.append("d");
			assertFalse(coro.isFinished());
Y
yunyao.zxl 已提交
69
			Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
70 71 72 73 74 75 76 77 78 79
		}
		seq.append("f");
		assertTrue(coro.isFinished());
		seq.append("g");
		assertEquals("abcdededefg", seq.toString());
	}


	@Test
	public void gcTest1() {
Y
yunyao.zxl 已提交
80 81
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
		Coroutine coro =  new Coroutine() {
Y
yunyao.zxl 已提交
82 83 84 85
			protected void run() {
				seq.append("c");
				Integer v1 = 1;
				Integer v2 = 14555668;
Y
yunyao.zxl 已提交
86
				yieldTo(threadCoro);
Y
yunyao.zxl 已提交
87 88 89 90 91 92
				seq.append("e");
				seq.append("(" + v1 + "," + v2 + ")");
			}
		};
		seq.append("b");
		System.gc();
Y
yunyao.zxl 已提交
93
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
94 95
		System.gc();
		seq.append("d");
Y
yunyao.zxl 已提交
96 97
		Coroutine.yieldTo(coro);
		seq.append("fg");
Y
yunyao.zxl 已提交
98 99 100 101 102
		assertEquals("abcde(1,14555668)fg", seq.toString());
	}

	@Test
	public void exceptionTest1() {
Y
yunyao.zxl 已提交
103
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
Y
yunyao.zxl 已提交
104 105 106 107 108 109 110 111 112 113 114
		Coroutine coro = new Coroutine() {
			protected void run() {
				seq.append("c");
				long temp = System.nanoTime();
				if (temp != 0)
					throw new RuntimeException();
				seq.append("e");
			}
		};
		seq.append("b");
		assertFalse(coro.isFinished());
Y
yunyao.zxl 已提交
115
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
116 117 118 119 120 121 122
		seq.append("d");
		seq.append("f");
		assertEquals("abcdf", seq.toString());
	}

	@Test
	public void largeStackframeTest() {
Y
yunyao.zxl 已提交
123 124
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
		Coroutine coro =  new Coroutine() {
Y
yunyao.zxl 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			protected void run() {
				seq.append("c");
				Integer v0 = 10000;
				Integer v1 = 10001;
				Integer v2 = 10002;
				Integer v3 = 10003;
				Integer v4 = 10004;
				Integer v5 = 10005;
				Integer v6 = 10006;
				Integer v7 = 10007;
				Integer v8 = 10008;
				Integer v9 = 10009;
				Integer v10 = 10010;
				Integer v11 = 10011;
				Integer v12 = 10012;
				Integer v13 = 10013;
				Integer v14 = 10014;
				Integer v15 = 10015;
				Integer v16 = 10016;
				Integer v17 = 10017;
				Integer v18 = 10018;
				Integer v19 = 10019;
Y
yunyao.zxl 已提交
147
				yieldTo(threadCoro);
Y
yunyao.zxl 已提交
148 149 150 151 152 153
				int sum = v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13 + v14 + v15 + v16 + v17 + v18 + v19;
				seq.append("e" + sum);
			}
		};
		seq.append("b");
		System.gc();
Y
yunyao.zxl 已提交
154
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
155 156
		System.gc();
		seq.append("d");
Y
yunyao.zxl 已提交
157
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
158 159 160 161 162 163
		seq.append("f");
		assertEquals("abcde200190f", seq.toString());
	}

	@Test
	public void shaTest() {
Y
yunyao.zxl 已提交
164
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
Y
yunyao.zxl 已提交
165 166 167 168 169 170
		Coroutine coro = new Coroutine(65536) {
			protected void run() {
				try {
					MessageDigest digest = MessageDigest.getInstance("SHA");
					digest.update("TestMessage".getBytes());
					seq.append("b");
Y
yunyao.zxl 已提交
171
					yieldTo(threadCoro);
Y
yunyao.zxl 已提交
172 173 174 175 176 177
					seq.append(digest.digest()[0]);
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				}
			}
		};
Y
yunyao.zxl 已提交
178
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
179 180
		seq.append("c");
		assertFalse(coro.isFinished());
Y
yunyao.zxl 已提交
181
		Coroutine.yieldTo(coro);
Y
yunyao.zxl 已提交
182 183 184 185 186
		assertTrue(coro.isFinished());
		assertEquals("abc72", seq.toString());
	}

	public void stackoverflowTest() {
Y
yunyao.zxl 已提交
187 188 189
		Coroutine threadCoro = Thread.currentThread().getCoroutineSupport().threadCoroutine();
		new Coroutine(255) {
			int i = 0;
Y
yunyao.zxl 已提交
190

Y
yunyao.zxl 已提交
191 192 193
			protected void run() {
				System.out.println("start");
				try {
Y
yunyao.zxl 已提交
194
					iter();
Y
yunyao.zxl 已提交
195 196
				} catch (StackOverflowError e) {
					System.out.println("i: " + i);
Y
yunyao.zxl 已提交
197
				}
Y
yunyao.zxl 已提交
198 199 200 201 202 203 204 205 206 207
				System.out.println("asdf");
			}

			private void iter() {
				System.out.print(".");
				i++;
				iter();
			}
		};
		Coroutine.yieldTo(threadCoro);
Y
yunyao.zxl 已提交
208
	}
Y
yunyao.zxl 已提交
209 210 211 212 213

  @Test
	public void destroyNonInitedTest() {
    new Coroutine();
  }
Y
yunyao.zxl 已提交
214
}