/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2018 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.cactoos.func;

import org.cactoos.Func;
import org.cactoos.matchers.RunsInThreads;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
 * Test case for {@link SolidBiFunc}.
 *
 * @author Roman Proshin (roman@proshin.org)
 * @version $Id$
 * @since 1.0
 * @checkstyle JavadocMethodCheck (500 lines)
 */
public final class SolidBiFuncTest {
    @Test
    public void testThatFuncIsSynchronized() {
        final int threads = 100;
        final int[] shared = new int[]{0};
        final SolidBiFunc<Integer, Integer, Boolean> testable =
            new SolidBiFunc<>(
                (first, second) -> {
                    shared[0] = shared[0] + 1;
                    return true;
                }
            );
        MatcherAssert.assertThat(
            func -> func.apply(true),
            new RunsInThreads<>(
                (Func<Boolean, Boolean>) input -> testable.apply(1, 1),
                threads
            )
        );
        MatcherAssert.assertThat(shared[0], Matchers.is(1));
    }

    @Test
    public void testThatFuncResultCacheIsLimited() throws Exception {
        final int[] executions = new int[]{0, 0};
        final SolidBiFunc<Integer, Integer, Boolean> func =
            new SolidBiFunc<>(
                (index, second) -> {
                    executions[index] = executions[index] + 1;
                    return true;
                },
                1
            );
        func.apply(0, 0);
        func.apply(1, 1);
        func.apply(1, 1);
        func.apply(0, 0);
        MatcherAssert.assertThat(executions[0], Matchers.is(2));
        MatcherAssert.assertThat(executions[1], Matchers.is(1));
    }
}

@@ -1175,16 +1175,78 @@
 .func;%0A%0A
+import java.security.SecureRandom;%0Aimport org.cactoos.BiFunc;%0A
 import o
@@ -1741,37 +1741,32 @@
 ;%0A        final 
-Solid
 BiFunc%3CInteger, 
@@ -2009,16 +2009,86 @@
 rtThat(%0A
+            %22SolidBiFunc can't work properly in concurrent threads.%22,%0A
         
@@ -2308,18 +2308,113 @@
 hat(
-shared%5B0%5D,
+%0A            %22Shared resource has been modified by multiple threads.%22,%0A            shared%5B0%5D,%0A           
  Mat
@@ -2416,32 +2416,41 @@
   Matchers.is(1)
+%0A        
 );%0A    %7D%0A%0A    @T
@@ -2541,63 +2541,8 @@
 nal 
-int%5B%5D executions = new int%5B%5D%7B0, 0%7D;%0A        final Solid
 BiFu
@@ -2562,23 +2562,23 @@
 nteger, 
-Boolean
+Integer
 %3E func =
@@ -2626,21 +2626,21 @@
        (
-index
+first
 , second
@@ -2648,72 +2648,37 @@
  -%3E 
-%7B%0A                    executions%5Bindex%5D = executions%5Bindex%5D + 1;
+new SecureRandom().nextInt(),
 %0A   
@@ -2694,40 +2694,33 @@
     
+1%0A
     
-return true;%0A
+ 
        
+);%0A
         
  %7D,%0A
@@ -2719,45 +2719,95 @@
     
- %7D,%0A                1%0A        
+MatcherAssert.assertThat(%0A            %22Result of (0, 0) call wasn't invalidated.%22,%0A
     
-);%0A
     
@@ -2826,25 +2826,18 @@
 ly(0, 0)
-;%0A      
  
++
  func.ap
@@ -2837,33 +2837,33 @@
 func.apply(1, 1)
-;
+,
 %0A        func.ap
@@ -2847,32 +2847,49 @@
 (1, 1),%0A        
+    Matchers.not(
 func.apply(1, 1)
@@ -2888,25 +2888,18 @@
 ly(1, 1)
-;%0A      
  
++
  func.ap
@@ -2911,137 +2911,18 @@
 , 0)
-;%0A        MatcherAssert.assertThat(executions%5B0%5D, Matchers.is(2));%0A        MatcherAssert.assertThat(executions%5B1%5D, Matchers.is(1)
+)%0A        
 );%0A 

Fix pull request comments