Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.services.simpleworkflow.flow.test;
 
 
 
 public class TestWorkflowClock implements WorkflowClock {
 
     private static final int SECOND = 1000;
 
     private static class TimerInfo<T> implements Comparable<TimerInfo<T>> {
 
         final Long fireTime;
 
         final Settable<T> timerResult = new Settable<T>();
 
         final T context;
 
 
         public TimerInfo(long fireTime, T context) {
             this. = fireTime;
             this. = context;
         }
 
         public void fire() {
             .set();
             if (.isReady()) {
                 .get().complete();
             }
             else {
                 new Task() {
 
                     @Override
                     protected void doExecute() throws Throwable {
                         .get().complete();
                     }
 
                 };
             }
         }
 
         public Promise<T> getResult() {
             return ;
         }
 
         @Override
         public int compareTo(TimerInfo<T> o) {
             return .compareTo(o.fireTime);
         }
 
         public void cancel() {
             .get().complete();
         }
 
         public long getFireTime() {
             return ;
         }
 
         public void setCompletionHandle(ExternalTaskCompletionHandle handle) {
             this..set(handle);
         }
         
         public void setResultDescription(String description) {
             .setDescription(description);
         }
     }
 
     private long clockTime = 0L;
 
     private PriorityQueue<TimerInfo<?>> timers = new PriorityQueue<TimerInfo<?>>();
 
     @Override
     public long currentTimeMillis() {
         return ;
     }
 
     public void setCurrentTimeMillis(long timeMillis) {
          = timeMillis;
    }
    @Override
    public boolean isReplaying() {
        // Unit test never replays
        return false;
    }
    @Override
    public Promise<VoidcreateTimer(long delaySeconds) {
        return createTimer(delaySecondsnull);
    }
    @Override
    public <T> Promise<T> createTimer(final long delaySecondsfinal T context) {
        if (delaySeconds < 0) {
            throw new IllegalArgumentException("negative delaySeconds");
        }
        if (delaySeconds == 0) {
            return Promise.asPromise(context);
        }
        long fireTime =  + delaySeconds * 1000;
        final TimerInfo<T> timer = new TimerInfo<T>(fireTimecontext);
        String timerName = "delay=" + delaySeconds;
        timer.setResultDescription("createTimer " + timerName);
        .add(timer);
        new ExternalTask() {
            @Override
            protected ExternalTaskCancellationHandler doExecute(ExternalTaskCompletionHandle handlethrows Throwable {
                timer.setCompletionHandle(handle);
                return new ExternalTaskCancellationHandler() {
                    @Override
                    public void handleCancellation(Throwable e) {
                        .remove(timer);
                        timer.cancel();
                    }
                };
            }
        }.setName(timerName);
        return timer.getResult();
    }
    public Long fireTimers() {
        while (true) {
            TimerInfo<?> timer = .peek();
            if (timer == null) {
                return null;
            }
            long timerTime = timer.getFireTime();
            if (timerTime > ) {
                return timerTime - ;
            }
            .poll();
            timer.fire();
        }
    }
    public void advanceSeconds(long seconds) {
        advanceMilliseconds(seconds * );
    }
    public void advanceMilliseconds(long milliseconds) {
         += milliseconds;
        while (true) {
            TimerInfo<?> timer = .peek();
            if (timer == null) {
                break;
            }
            long timerTime = timer.getFireTime();
            if (timerTime > ) {
                break;
            }
            timer.fire();
            .poll();
        }
    }
New to GrepCode? Check out our FAQ X