/*
** Module   : WaitEventSem.java
** Abstract : directive to wait for an event semaphore to be posted
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090224 Directive to wait for an event semaphore to be posted.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import com.goldencode.util.*;
import com.goldencode.harness.*;

/**
 * Directive to wait for an event semaphore to be posted.
 */
public class WaitEventSem
extends Report
implements Testable
{
   /** Short name for the activity. */
   private static final String NAME = "WAIT-EVENT-SEM";
   
   /** The event semaphore to utilize. */
   private final EventSemaphore event;
   
   /** The number of milliseconds before a timeout occurs. */
   private final int timeout;
   
   /**
    * Constructor.
    *
    * @param    name
    *           Event semaphore name.
    * @param    event
    *           The event semaphore to use.
    * @param    timeout
    *           The number of milliseconds before a timeout occurs during the
    *           wait.
    */
   public WaitEventSem(String name, EventSemaphore event, int timeout)
   {
      super(NAME, String.format("name = '%s'; timeout = %d", name, timeout));
      this.event   = event;
      this.timeout = timeout;
   }
   
   /**
    * Execute the directive of the test using the given terminal as the data
    * sink and/or source for the operation.
    */
   @Override
   public void execute()
   {
      String reason = null;
      
      try
      {
         if (!event.waitFor(timeout))
         {
            reason = "Timeout while waiting for event semaphore to be posted."; 
         }
      }
      
      catch (InterruptedException ie)
      {
         reason = "Interrupted during wait for event semaphore to be posted.";
      }
      
      setStatus((reason == null) ? Status.PASSED : Status.FAILED, reason);
   }
}
