Commit e9490857 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

added a test

parent a7d402e9
Loading
Loading
Loading
Loading
+64 −0
Original line number Diff line number Diff line
package sandbox

import "errors"

type Sandbox struct {
	Options       *Options
	stopStream    chan bool
	stoppedStream chan bool
}

type Options struct {
}

func New(options *Options) *Sandbox {
	return &Sandbox{
		Options:       options,
		stopStream:    make(chan bool),
		stoppedStream: make(chan bool),
	}
}

func (s *Sandbox) Start() error {
	startedStream := make(chan bool)
	defer close(startedStream)

	go s.loop(startedStream)

	<-startedStream

	return nil
}

func (s *Sandbox) Stop() error {
	if s.stopStream == nil {
		return errors.New("no stop stream")
	}

	s.stopStream <- true
	<-s.stoppedStream

	return nil
}

func (s *Sandbox) loop(startedStream chan<- bool) {
	defer func() {
		close(s.stopStream)
		s.stopStream = nil
	}()

	startedStream <- true

	for {
		select {
		case stop, valid := <-s.stopStream:
			if !valid {
				continue
			}
			if !stop {
				continue
			}
			s.stoppedStream <- true
			return
		}
	}
}
+18 −3
Original line number Diff line number Diff line
package sandbox

import "testing"
import (
	"testing"
	"time"
)

func TestSkip(t *testing.T) {
	t.Skip("not implemented")
func TestSandboxGeneric(t *testing.T) {
	o := &Options{}

	s := New(o)

	if err := s.Start(); err != nil {
		t.Fatalf("Start() failed with: %s", err)
	}

	time.Sleep(1 * time.Second)

	if err := s.Stop(); err != nil {
		t.Fatalf("Stop() failed with: %s", err)
	}
}