blob: 7c44bb1fa418d6c8fce36c240b6c6dabc7f35ee5 [file] [log] [blame]
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed 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 android.virt.test;
import org.junit.Test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.android.tradefed.log.LogUtil.CLog;
public class VsockTest extends VirtHostTestCase {
private static final String LOG_TAG = "VsockTest";
private static final String TEST_NAME = "VsockServer";
private static final Long TEST_TIMEOUT = 20L;
private static final TimeUnit TEST_TIMEOUT_UNIT = TimeUnit.SECONDS;
@Test
public void testVsockServer() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(2);
prepareGuestVM();
try {
Future<?> vmTask = executor.submit(new GuestVmTask());
Future<Void> hostTask = executor.submit(new HostBinaryTask());
hostTask.get(TEST_TIMEOUT, TEST_TIMEOUT_UNIT);
} finally {
executor.shutdown();
executor.awaitTermination(TEST_TIMEOUT, TEST_TIMEOUT_UNIT);
finishGuestVM();
}
}
class GuestVmTask implements Callable<Void> {
@Override
public Void call() throws Exception {
runGuestVM(TEST_NAME, /* CID */ 42);
return null;
}
}
class HostBinaryTask implements Callable<Void> {
@Override
public Void call() throws Exception {
while (true) {
Thread.sleep(2000);
CLog.w("Executing host binary...");
if (runHostBinary(TEST_NAME)) {
return null;
}
}
}
}
}