<commit_before>package fake_bbs

import (
	"sync"
	"time"

	"github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/models"
)

type FakeExecutorBBS struct {
	callsToConverge int

	desiredTaskChan     chan models.Task
	desiredTaskStopChan chan bool
	desiredTaskErrChan  chan error

	desiredLrpChan chan models.TransitionalLongRunningProcess

	maintainConvergeInterval      time.Duration
	maintainConvergeExecutorID    string
	maintainConvergeStatusChannel <-chan bool
	maintainConvergeStopChannel   chan<- chan bool
	maintainConvergeLockError     error

	MaintainExecutorPresenceInputs struct {
		HeartbeatInterval chan time.Duration
		ExecutorID        chan string
	}
	MaintainExecutorPresenceOutputs struct {
		Presence *FakePresence
		Error    error
	}

	claimedTasks []models.Task
	claimTaskErr error

	startedTasks []models.Task
	startTaskErr error

	completedTasks           []models.Task
	completeTaskErr          error
	convergeTimeToClaimTasks time.Duration

	startedLrps []models.TransitionalLongRunningProcess
	startLrpErr error

	sync.RWMutex
}

func NewFakeExecutorBBS() *FakeExecutorBBS {
	fakeBBS := &FakeExecutorBBS{}
	fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval = make(chan time.Duration, 1)
	fakeBBS.MaintainExecutorPresenceInputs.ExecutorID = make(chan string, 1)
	fakeBBS.desiredTaskChan = make(chan models.Task, 1)
	fakeBBS.desiredTaskStopChan = make(chan bool)
	fakeBBS.desiredTaskErrChan = make(chan error)
	fakeBBS.desiredLrpChan = make(chan models.TransitionalLongRunningProcess, 1)
	return fakeBBS
}

func (fakeBBS *FakeExecutorBBS) MaintainExecutorPresence(heartbeatInterval time.Duration, executorID string) (bbs.Presence, <-chan bool, error) {
	fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval <- heartbeatInterval
	fakeBBS.MaintainExecutorPresenceInputs.ExecutorID <- executorID

	presence := fakeBBS.MaintainExecutorPresenceOutputs.Presence

	if presence == nil {
		presence = &FakePresence{
			MaintainStatus: true,
		}
	}

	status, _ := presence.Maintain(heartbeatInterval)

	return presence, status, fakeBBS.MaintainExecutorPresenceOutputs.Error
}

func (fakeBBS *FakeExecutorBBS) GetMaintainExecutorPresenceHeartbeatInterval() time.Duration {
	return <-fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval
}

func (fakeBBS *FakeExecutorBBS) GetMaintainExecutorPresenceId() string {
	return <-fakeBBS.MaintainExecutorPresenceInputs.ExecutorID
}

func (fakeBBS *FakeExecutorBBS) WatchForDesiredTask() (<-chan models.Task, chan<- bool, <-chan error) {
	return fakeBBS.desiredTaskChan, fakeBBS.desiredTaskStopChan, fakeBBS.desiredTaskErrChan
}

func (fakeBBS *FakeExecutorBBS) WatchForDesiredTransitionalLongRunningProcess() (<-chan models.TransitionalLongRunningProcess, chan<- bool, <-chan error) {
	return fakeBBS.desiredLrpChan, nil, nil
}

func (fakeBBS *FakeExecutorBBS) EmitDesiredTask(task models.Task) {
	fakeBBS.desiredTaskChan <- task
}

func (fakeBBS *FakeExecutorBBS) ClaimTask(task models.Task, executorID string) (models.Task, error) {
	task.ExecutorID = executorID

	fakeBBS.RLock()
	err := fakeBBS.claimTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	fakeBBS.Lock()
	fakeBBS.claimedTasks = append(fakeBBS.claimedTasks, task)
	fakeBBS.Unlock()

	return task, nil
}

func (fakeBBS *FakeExecutorBBS) ClaimedTasks() []models.Task {
	fakeBBS.RLock()
	defer fakeBBS.RUnlock()

	claimed := make([]models.Task, len(fakeBBS.claimedTasks))
	copy(claimed, fakeBBS.claimedTasks)

	return claimed
}

func (fakeBBS *FakeExecutorBBS) SetClaimTaskErr(err error) {
	fakeBBS.Lock()
	defer fakeBBS.Unlock()

	fakeBBS.claimTaskErr = err
}

func (fakeBBS *FakeExecutorBBS) StartTask(task models.Task, containerHandle string) (models.Task, error) {
	fakeBBS.RLock()
	err := fakeBBS.startTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	task.ContainerHandle = containerHandle

	fakeBBS.Lock()
	fakeBBS.startedTasks = append(fakeBBS.startedTasks, task)
	fakeBBS.Unlock()

	return task, nil
}

func (fakeBBS *FakeExecutorBBS) StartTransitionalLongRunningProcess(lrp models.TransitionalLongRunningProcess) error {
	fakeBBS.RLock()
	err := fakeBBS.startLrpErr
	fakeBBS.RUnlock()

	if err != nil {
		return err
	}

	fakeBBS.Lock()
	fakeBBS.startedLrps = append(fakeBBS.startedLrps, lrp)
	fakeBBS.Unlock()

	return nil
}

func (fakeBBS *FakeExecutorBBS) StartedTasks() []models.Task {
	fakeBBS.RLock()
	defer fakeBBS.RUnlock()

	started := make([]models.Task, len(fakeBBS.startedTasks))
	copy(started, fakeBBS.startedTasks)

	return started
}

func (fakeBBS *FakeExecutorBBS) SetStartTaskErr(err error) {
	fakeBBS.Lock()
	defer fakeBBS.Unlock()

	fakeBBS.startTaskErr = err
}

func (fakeBBS *FakeExecutorBBS) CompleteTask(task models.Task, failed bool, failureReason string, result string) (models.Task, error) {
	fakeBBS.RLock()
	err := fakeBBS.completeTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	task.Failed = failed
	task.FailureReason = failureReason
	task.Result = result

	fakeBBS.Lock()<commit_msg>Add lrp-related methods to fake executor bbs<commit_after>package fake_bbs

import (
	"sync"
	"time"

	"github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/models"
)

type FakeExecutorBBS struct {
	callsToConverge int

	desiredTaskChan     chan models.Task
	desiredTaskStopChan chan bool
	desiredTaskErrChan  chan error

	desiredLrpChan chan models.TransitionalLongRunningProcess

	maintainConvergeInterval      time.Duration
	maintainConvergeExecutorID    string
	maintainConvergeStatusChannel <-chan bool
	maintainConvergeStopChannel   chan<- chan bool
	maintainConvergeLockError     error

	MaintainExecutorPresenceInputs struct {
		HeartbeatInterval chan time.Duration
		ExecutorID        chan string
	}
	MaintainExecutorPresenceOutputs struct {
		Presence *FakePresence
		Error    error
	}

	claimedTasks []models.Task
	claimTaskErr error

	startedTasks []models.Task
	startTaskErr error

	completedTasks           []models.Task
	completeTaskErr          error
	convergeTimeToClaimTasks time.Duration

	startedLrps []models.TransitionalLongRunningProcess
	startLrpErr error

	sync.RWMutex
}

func NewFakeExecutorBBS() *FakeExecutorBBS {
	fakeBBS := &FakeExecutorBBS{}
	fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval = make(chan time.Duration, 1)
	fakeBBS.MaintainExecutorPresenceInputs.ExecutorID = make(chan string, 1)
	fakeBBS.desiredTaskChan = make(chan models.Task, 1)
	fakeBBS.desiredTaskStopChan = make(chan bool)
	fakeBBS.desiredTaskErrChan = make(chan error)
	fakeBBS.desiredLrpChan = make(chan models.TransitionalLongRunningProcess, 1)
	return fakeBBS
}

func (fakeBBS *FakeExecutorBBS) MaintainExecutorPresence(heartbeatInterval time.Duration, executorID string) (bbs.Presence, <-chan bool, error) {
	fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval <- heartbeatInterval
	fakeBBS.MaintainExecutorPresenceInputs.ExecutorID <- executorID

	presence := fakeBBS.MaintainExecutorPresenceOutputs.Presence

	if presence == nil {
		presence = &FakePresence{
			MaintainStatus: true,
		}
	}

	status, _ := presence.Maintain(heartbeatInterval)

	return presence, status, fakeBBS.MaintainExecutorPresenceOutputs.Error
}

func (fakeBBS *FakeExecutorBBS) GetMaintainExecutorPresenceHeartbeatInterval() time.Duration {
	return <-fakeBBS.MaintainExecutorPresenceInputs.HeartbeatInterval
}

func (fakeBBS *FakeExecutorBBS) GetMaintainExecutorPresenceId() string {
	return <-fakeBBS.MaintainExecutorPresenceInputs.ExecutorID
}

func (fakeBBS *FakeExecutorBBS) WatchForDesiredTask() (<-chan models.Task, chan<- bool, <-chan error) {
	return fakeBBS.desiredTaskChan, fakeBBS.desiredTaskStopChan, fakeBBS.desiredTaskErrChan
}

func (fakeBBS *FakeExecutorBBS) WatchForDesiredTransitionalLongRunningProcess() (<-chan models.TransitionalLongRunningProcess, chan<- bool, <-chan error) {
	return fakeBBS.desiredLrpChan, nil, nil
}

func (fakeBBS *FakeExecutorBBS) EmitDesiredTask(task models.Task) {
	fakeBBS.desiredTaskChan <- task
}

func (fakeBBS *FakeExecutorBBS) EmitDesiredLrp(lrp models.TransitionalLongRunningProcess) {
	fakeBBS.desiredLrpChan <- lrp
}

func (fakeBBS *FakeExecutorBBS) ClaimTask(task models.Task, executorID string) (models.Task, error) {
	task.ExecutorID = executorID

	fakeBBS.RLock()
	err := fakeBBS.claimTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	fakeBBS.Lock()
	fakeBBS.claimedTasks = append(fakeBBS.claimedTasks, task)
	fakeBBS.Unlock()

	return task, nil
}

func (fakeBBS *FakeExecutorBBS) ClaimedTasks() []models.Task {
	fakeBBS.RLock()
	defer fakeBBS.RUnlock()

	claimed := make([]models.Task, len(fakeBBS.claimedTasks))
	copy(claimed, fakeBBS.claimedTasks)

	return claimed
}

func (fakeBBS *FakeExecutorBBS) SetClaimTaskErr(err error) {
	fakeBBS.Lock()
	defer fakeBBS.Unlock()

	fakeBBS.claimTaskErr = err
}

func (fakeBBS *FakeExecutorBBS) StartTask(task models.Task, containerHandle string) (models.Task, error) {
	fakeBBS.RLock()
	err := fakeBBS.startTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	task.ContainerHandle = containerHandle

	fakeBBS.Lock()
	fakeBBS.startedTasks = append(fakeBBS.startedTasks, task)
	fakeBBS.Unlock()

	return task, nil
}

func (fakeBBS *FakeExecutorBBS) StartTransitionalLongRunningProcess(lrp models.TransitionalLongRunningProcess) error {
	fakeBBS.RLock()
	err := fakeBBS.startLrpErr
	fakeBBS.RUnlock()

	if err != nil {
		return err
	}

	fakeBBS.Lock()
	fakeBBS.startedLrps = append(fakeBBS.startedLrps, lrp)
	fakeBBS.Unlock()

	return nil
}

func (fakeBBS *FakeExecutorBBS) StartedTasks() []models.Task {
	fakeBBS.RLock()
	defer fakeBBS.RUnlock()

	started := make([]models.Task, len(fakeBBS.startedTasks))
	copy(started, fakeBBS.startedTasks)

	return started
}

func (fakeBBS *FakeExecutorBBS) StartedLongRunningProcesses() []models.TransitionalLongRunningProcess {
	fakeBBS.RLock()
	defer fakeBBS.RUnlock()

	started := make([]models.TransitionalLongRunningProcess, len(fakeBBS.startedLrps))
	copy(started, fakeBBS.startedLrps)

	return started
}

func (fakeBBS *FakeExecutorBBS) SetStartTaskErr(err error) {
	fakeBBS.Lock()
	defer fakeBBS.Unlock()

	fakeBBS.startTaskErr = err
}

func (fakeBBS *FakeExecutorBBS) SetStartLrpErr(err error) {
	fakeBBS.Lock()
	defer fakeBBS.Unlock()

	fakeBBS.startLrpErr = err
}

func (fakeBBS *FakeExecutorBBS) CompleteTask(task models.Task, failed bool, failureReason string, result string) (models.Task, error) {
	fakeBBS.RLock()
	err := fakeBBS.completeTaskErr
	fakeBBS.RUnlock()

	if err != nil {
		return task, err
	}

	task.Failed = failed
	task.FailureReason = failureReason
	task.Result = result

	fakeBBS.Lock()
{'fraction_non_alphanumeric': 0.09305802111557507, 'fraction_numerical': 0.0007474539848640568, 'mean_word_length': 5.623762376237623, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2115684', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3636, 'n_words': 713}