1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.incubator.http.internal.websocket;
  25 
  26 import jdk.incubator.http.internal.common.Pair;
  27 import jdk.incubator.http.internal.common.SequentialScheduler;
  28 import jdk.incubator.http.internal.common.SequentialScheduler.DeferredCompleter;
  29 
  30 import java.util.Arrays;
  31 import java.util.Iterator;
  32 import java.util.concurrent.CompletableFuture;
  33 import java.util.concurrent.CompletionStage;
  34 import java.util.function.Consumer;
  35 
  36 public class MockReceiver extends Receiver {
  37 
  38     private final Iterator<Pair<CompletionStage<?>, Consumer<MessageStreamConsumer>>> iterator;
  39     private final MessageStreamConsumer consumer;
  40 
  41     public MockReceiver(MessageStreamConsumer consumer, RawChannel channel,
  42                         Pair<CompletionStage<?>, Consumer<MessageStreamConsumer>>... pairs) {
  43         super(consumer, channel);
  44         this.consumer = consumer;
  45         iterator = Arrays.asList(pairs).iterator();
  46     }
  47 
  48     @Override
  49     protected SequentialScheduler createScheduler() {
  50         class X { // Class is hack needed to allow the task to refer to the scheduler
  51             SequentialScheduler scheduler = new SequentialScheduler(task());
  52 
  53             SequentialScheduler.RestartableTask task() {
  54                 return new SequentialScheduler.RestartableTask() {
  55                     @Override
  56                     public void run(DeferredCompleter taskCompleter) {
  57                         if (!scheduler.isStopped() && !demand.isFulfilled()) {
  58                             if (!iterator.hasNext()) {
  59                                 taskCompleter.complete();
  60                                 return;
  61                             }
  62                             Pair<CompletionStage<?>, Consumer<MessageStreamConsumer>> p = iterator.next();
  63                             CompletableFuture<?> cf = p.first.toCompletableFuture();
  64                             if (cf.isDone()) { // Forcing synchronous execution
  65                                 p.second.accept(consumer);
  66                                 repeat(taskCompleter);
  67                             } else {
  68                                 cf.whenCompleteAsync((r, e) -> {
  69                                     p.second.accept(consumer);
  70                                     repeat(taskCompleter);
  71                                 });
  72                             }
  73                         } else {
  74                             taskCompleter.complete();
  75                         }
  76                     }
  77 
  78                     private void repeat(DeferredCompleter taskCompleter) {
  79                         taskCompleter.complete();
  80                         scheduler.runOrSchedule();
  81                     }
  82                 };
  83             }
  84         }
  85         return new X().scheduler;
  86     }
  87 }