1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.cluster; 17 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertTrue; 20 21 import com.android.tradefed.cluster.ClusterEventUploaderTest.Event; 22 23 import org.junit.Ignore; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 import java.io.IOException; 29 import java.util.List; 30 import java.util.concurrent.locks.Lock; 31 import java.util.concurrent.locks.ReentrantLock; 32 33 /** Function tests for {@link ClusterEventUploader}. */ 34 @RunWith(JUnit4.class) 35 public class ClusterEventUploaderFuncTest { 36 37 private ClusterEventUploader<Event> mUploader; 38 39 @Ignore 40 @Test testPostCommandEvent_multipleThread()41 public void testPostCommandEvent_multipleThread() throws Exception { 42 final Event event1 = new Event("event1"); 43 final Event event2 = new Event("event2"); 44 45 final Lock lock = new ReentrantLock(); 46 47 mUploader = 48 new ClusterEventUploader<Event>() { 49 @Override 50 protected void doUploadEvents(List<Event> events) throws IOException { 51 try { 52 lock.lock(); 53 } finally { 54 lock.unlock(); 55 } 56 } 57 }; 58 59 Thread thread1 = 60 new Thread( 61 new Runnable() { 62 @Override 63 public void run() { 64 mUploader.postEvent(event1); 65 // Thread1 uses flush will be blocked. 66 mUploader.flush(); 67 } 68 }); 69 Thread thread2 = 70 new Thread( 71 new Runnable() { 72 @Override 73 public void run() { 74 mUploader.postEvent(event2); 75 // Thread2 doesn't use flush will not be blocked. 76 } 77 }); 78 // Thread1 will be blocked. Thread2 will not be blocked. 79 lock.lock(); 80 thread1.start(); 81 thread2.start(); 82 thread2.join(60 * 1000); // timeout in milliseconds 83 assertTrue(thread1.isAlive()); 84 assertFalse(thread2.isAlive()); 85 // Unblock thread 1. 86 lock.unlock(); 87 thread1.join(60 * 1000); // timeout in milliseconds 88 assertFalse(thread1.isAlive()); 89 } 90 } 91