package ru.job4j3.threads.pool;

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

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import static org.junit.Assert.assertThat;
import static org.hamcrest.core.Is.is;

/**
 * Class SwitcherTest to test Switcher.
 *
 * @author v.ivanov
 * @version 1
 * @since 28.04.2019
 */
public class SwitcherTest {

    /** Field Switcher instance.*/
    private Switcher switcher;

    /**
     * Initialization section.
     */
    @Before
    public void setUp() {
        this.switcher = new Switcher("String line with number ");
    }

    /**
     * Method to check Switcher addToString() method.
     */
    @Test
    public void checkSwitcherAddToStringMethod() {
        this.switcher.addToString(555);
        assertThat(this.switcher.getValue(), is("String line with number 555"));
    }

    /**
     * Method to check Switcher with two threads.
     */
    @Test
    public void checkSwitcherAddToStringWithTwoThreads() {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Semaphore sem1 = new Semaphore(0);
        Semaphore sem2 = new Semaphore(1);
        final int CYCLE = 3;
        Runnable task1 = () -> {
            try {
                sem2.acquire();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            IntStream.range(0, 10).forEach(i -> this.switcher.addToString(1));
            sem1.release();
        };
        Runnable task2 = () -> {
            try {
                sem1.acquire();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            IntStream.range(0, 10).forEach(i -> this.switcher.addToString(2));
            sem2.release();
        };
        IntStream.range(0, CYCLE).forEach(i -> {
            executor.submit(task1);
            executor.submit(task2);
        });
        try {
            executor.shutdown();
            executor.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            System.err.println("Termination interrupted");
        } finally {
            executor.shutdown();
        }
        assertThat(switcher.getValue(), is("String line with number 111111111122222222221111111111222222222211111111112222222222"));
    }
}

@@ -77,93 +77,8 @@
 est;
-%0A%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;
 %0Aimp
@@ -117,46 +117,8 @@
 re;%0A
-import java.util.concurrent.TimeUnit;%0A
 impo
@@ -990,76 +990,8 @@
 ) %7B%0A
-        ExecutorService executor = Executors.newFixedThreadPool(2);%0A
     
@@ -1113,33 +1113,96 @@
     
-Runnable task1 = () -%3E %7B%0A
+Thread t1 = new Thread(() -%3E %7B%0A            IntStream.range(0, CYCLE).forEach(i -%3E %7B%0A    
     
@@ -1227,24 +1227,28 @@
             
+   
+ 
 sem2.acquire
@@ -1243,32 +1243,36 @@
 sem2.acquire();%0A
+    
             %7D ca
@@ -1310,32 +1310,36 @@
                 
+  
+  
 throw new Illega
@@ -1362,34 +1362,40 @@
 e);%0A            
-%7D%0A
+    %7D%0A  
             IntS
@@ -1382,32 +1382,34 @@
 %7D%0A              
+  
 IntStream.range(
@@ -1416,32 +1416,33 @@
 0, 10).forEach(i
+2
  -%3E this.switche
@@ -1468,24 +1468,28 @@
             
+ 
+   
 sem1.release
@@ -1492,33 +1492,38 @@
 ease();%0A        
+ 
-%7D
+   %7D)
 ;%0A        Runnab
@@ -1520,33 +1520,108 @@
     
-Runnable task2 = () -%3E %7B%0A
+%7D);%0A        Thread t2 = new Thread(() -%3E %7B%0A            IntStream.range(0, CYCLE).forEach(i -%3E %7B%0A    
     
@@ -1646,24 +1646,28 @@
             
+  
+  
 sem1.acquire
@@ -1662,32 +1662,36 @@
 sem1.acquire();%0A
+    
             %7D ca
@@ -1729,32 +1729,36 @@
                 
+ 
+   
 throw new Illega
@@ -1781,32 +1781,36 @@
 e);%0A            
+    
 %7D%0A            In
@@ -1799,32 +1799,36 @@
   %7D%0A            
+ 
+   
 IntStream.range(
@@ -1839,24 +1839,25 @@
 0).forEach(i
+2
  -%3E this.swi
@@ -1875,24 +1875,28 @@
 String(2));%0A
+    
             
@@ -1919,17 +1919,22 @@
         
-%7D
+    %7D)
 ;%0A      
@@ -1939,130 +1939,49 @@
     
-IntStream.range(0, CYCLE).forEach(i -%3E %7B%0A            executor.submit(task1);%0A            executor.submit(task2);%0A        %7D
+%7D);%0A        t1.start();%0A        t2.start(
 );%0A 
@@ -2005,32 +2005,22 @@
         
-executor.shutdow
+t1.joi
 n();%0A   
@@ -2032,54 +2032,16 @@
     
-executor.awaitTermination(10, TimeUnit.SECONDS
+t2.join(
 );%0A 
@@ -2098,104 +2098,25 @@
     
-System.err.println(%22Termination interrupted%22);%0A        %7D finally %7B%0A            executor.shutdown
+e.printStackTrace
 ();%0A
@@ -2144,16 +2144,21 @@
 ertThat(
+this.
 switcher

Fix SwitcherTest without executor