/*
Copyright 2020 The Kubernetes Authors.
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 io.kubernetes.client.informer.cache;

import static org.junit.Assert.*;

import io.kubernetes.client.informer.ResourceEventHandler;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Pod;
import org.junit.Test;

public class ProcessorListenerTest {

  private static boolean addNotificationReceived;
  private static boolean updateNotificationReceived;
  private static boolean deleteNotificationReceived;

  @Test
  public void testNotificationHandling() throws InterruptedException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default"));

    ProcessorListener<V1Pod> listener =
        new ProcessorListener<>(
            new ResourceEventHandler<V1Pod>() {

              @Override
              public void onAdd(V1Pod obj) {
                assertEquals(pod, obj);
                addNotificationReceived = true;
              }

              @Override
              public void onUpdate(V1Pod oldObj, V1Pod newObj) {
                assertEquals(pod, newObj);
                updateNotificationReceived = true;
              }

              @Override
              public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
                assertEquals(pod, obj);
                deleteNotificationReceived = true;
              }
            },
            0);

    listener.add(new ProcessorListener.AddNotification<>(pod));
    listener.add(new ProcessorListener.UpdateNotification<>(null, pod));
    listener.add(new ProcessorListener.DeleteNotification<>(pod));

    Thread listenerThread = new Thread(listener::run);
    listenerThread.setDaemon(true);
    listenerThread.start();

    // sleep 1s for consuming nofications from queue
    Thread.sleep(1000);

    assertTrue(addNotificationReceived);
    assertTrue(updateNotificationReceived);
    assertTrue(deleteNotificationReceived);
  }

  @Test
  public void testMultipleNotificationsHandling() throws InterruptedException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default"));
    final int[] count = {0};

    ProcessorListener<V1Pod> listener =
        new ProcessorListener<>(
            new ResourceEventHandler<V1Pod>() {
              @Override
              public void onAdd(V1Pod obj) {
                assertEquals(pod, obj);
                count[0]++;
              }

              @Override
              public void onUpdate(V1Pod oldObj, V1Pod newObj) {}

              @Override
              public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
            },
            0);

    for (int i = 0; i < 2000; i++) {
      listener.add(new ProcessorListener.AddNotification<>(pod));
    }

    Thread listenerThread = new Thread(listener);
    listenerThread.setDaemon(true);
    listenerThread.start();

    Thread.sleep(2000);

    assertEquals(count[0], 2000);
  }
}

@@ -633,17 +633,71 @@
 .Assert.
-*
+assertEquals;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0Aimpor
@@ -857,16 +857,60 @@
 .V1Pod;%0A
+import java.util.concurrent.CountDownLatch;%0A
 import o
@@ -1287,24 +1287,81 @@
 efault%22));%0A%0A
+    final CountDownLatch cLatch = new CountDownLatch(3);%0A
     Processo
@@ -1619,32 +1619,68 @@
 eceived = true;%0A
+                cLatch.countDown();%0A
               %7D%0A
@@ -1855,32 +1855,68 @@
 eceived = true;%0A
+                cLatch.countDown();%0A
               %7D%0A
@@ -2105,32 +2105,68 @@
 eceived = true;%0A
+                cLatch.countDown();%0A
               %7D%0A
@@ -2533,33 +2533,38 @@
  // 
-sleep 1s for consuming
+wait until consumption of
  no
+ti
 fica
@@ -2588,25 +2588,21 @@
     
-Thread.sleep(1000
+cLatch.await(
 );%0A%0A
@@ -2944,16 +2944,123 @@
 = %7B0%7D;%0A%0A
+    int notificationCount = 2000;%0A    final CountDownLatch cLatch = new CountDownLatch(notificationCount);%0A
     Proc
@@ -3309,16 +3309,52 @@
 t%5B0%5D++;%0A
+                cLatch.countDown();%0A
         
@@ -3616,20 +3616,33 @@
  0; i %3C 
-2000
+notificationCount
 ; i++) %7B
@@ -3838,25 +3838,21 @@
     
-Thread.sleep(2000
+cLatch.await(
 );%0A%0A

remove Thread.sleep in ProcessorListenerTest.java