github.com/wtsi-hgi/go-softpack-builder@v1.8.1/internal/wrmock/mock.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2024 Genome Research Ltd.
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining
     5   * a copy of this software and associated documentation files (the
     6   * "Software"), to deal in the Software without restriction, including
     7   * without limitation the rights to use, copy, modify, merge, publish,
     8   * distribute, sublicense, and/or sell copies of the Software, and to
     9   * permit persons to whom the Software is furnished to do so, subject to
    10   * the following conditions:
    11   *
    12   * The above copyright notice and this permission notice shall be included
    13   * in all copies or substantial portions of the Software.
    14   *
    15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    16   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    17   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    18   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    19   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    20   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    21   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22   ******************************************************************************/
    23  
    24  package wrmock
    25  
    26  import (
    27  	"sync"
    28  	"time"
    29  
    30  	"github.com/wtsi-hgi/go-softpack-builder/wr"
    31  )
    32  
    33  // MockWR can be used to test a build.Builder without having real wr running.
    34  type MockWR struct {
    35  	Cmd                   string
    36  	Fail                  bool
    37  	PollForStatusInterval time.Duration
    38  	JobDuration           time.Duration
    39  
    40  	sync.RWMutex
    41  	ReturnStatus wr.WRJobStatus
    42  }
    43  
    44  // NewMockWR returns a new MockWR that will wait pollForStatusInterval during
    45  // WaitForRunning() and jobDuration during Wait().
    46  func NewMockWR(pollForStatusInterval, jobDuration time.Duration) *MockWR {
    47  	return &MockWR{
    48  		PollForStatusInterval: pollForStatusInterval,
    49  		JobDuration:           jobDuration,
    50  	}
    51  }
    52  
    53  // Add implements build.Runner interface.
    54  func (m *MockWR) Add(cmd string) (string, error) { //nolint:unparam
    55  	m.Lock()
    56  	defer m.Unlock()
    57  
    58  	m.Cmd = cmd
    59  
    60  	return "abc123", nil
    61  }
    62  
    63  // SetRunning can be used to mock a job that started running.
    64  func (m *MockWR) SetRunning() {
    65  	m.Lock()
    66  	defer m.Unlock()
    67  
    68  	m.ReturnStatus = wr.WRJobStatusRunning
    69  }
    70  
    71  // SetComplete can be used to mock a job that finished running.
    72  func (m *MockWR) SetComplete() {
    73  	m.Lock()
    74  	defer m.Unlock()
    75  
    76  	m.ReturnStatus = wr.WRJobStatusComplete
    77  }
    78  
    79  // GetLastCmd returns the last cmd Add()ed.
    80  func (m *MockWR) GetLastCmd() string {
    81  	m.RLock()
    82  	defer m.RUnlock()
    83  
    84  	return m.Cmd
    85  }
    86  
    87  // WaitForRunning implements build.Runner interface.
    88  func (m *MockWR) WaitForRunning(string) error { //nolint:unparam
    89  	for {
    90  		m.RLock()
    91  		rs := m.ReturnStatus
    92  		m.RUnlock()
    93  
    94  		if rs == wr.WRJobStatusRunning || rs == wr.WRJobStatusBuried || rs == wr.WRJobStatusComplete {
    95  			return nil
    96  		}
    97  
    98  		<-time.After(m.PollForStatusInterval)
    99  	}
   100  }
   101  
   102  // Wait implements build.Runner interface.
   103  func (m *MockWR) Wait(string) (wr.WRJobStatus, error) { //nolint:unparam
   104  	<-time.After(m.JobDuration)
   105  
   106  	if m.Fail {
   107  		return wr.WRJobStatusBuried, nil
   108  	}
   109  
   110  	return wr.WRJobStatusComplete, nil
   111  }
   112  
   113  // Status implements build.Runner interface.
   114  func (m *MockWR) Status(string) (wr.WRJobStatus, error) { //nolint:unparam
   115  	m.RLock()
   116  	defer m.RUnlock()
   117  
   118  	return m.ReturnStatus, nil
   119  }