func (t *TurnRunnerSuite) NewResultSuccess() async.R {
	r, s := async.NewR()
	async.When(async.Done(), func() {
		s.Complete()
	})
	return r
}
Beispiel #2
0
// WhenErrorReturnCoverage provides coverage for the When function.
func (t *RSuite) WhenErrorReturnCoverage() async.R {
	expected := fmt.Errorf("some error")
	w := async.When(async.Done(), func() async.R {
		r, s := async.NewR()
		s.Fail(expected)
		return r
	})

	w1 := async.When(w, func(err error) (interface{}, error) {
		if err != expected {
			return nil, fmt.Errorf("Expected executed w.  Got: %v, Want: %v", err, expected)
		}
		return nil, err
	})

	w2 := async.When(w1, func(err error) error {
		if err != expected {
			return fmt.Errorf("Expected executed w1.  Got: %v, Want: %v", err, expected)
		}
		return err
	})

	return async.When(w2, func(err error) async.R {
		if err != expected {
			return async.NewErrorf("Expected executed w2.  Got: %v, Want: %v", err, expected)
		}
		return async.Done()
	})
}
Beispiel #3
0
// WhenErrorCoverage provides coverage for the When function.
func (t *RSuite) WhenErrorCoverage() async.R {
	expected := fmt.Errorf("some error")
	w := async.When(async.Done(), func() async.R {
		r, s := async.NewR()
		s.Fail(expected)
		return r
	})

	w1 := async.When(w, func(_ interface{}, err error) async.R {
		if err != expected {
			return async.NewErrorf("Expected executed w.  Got: %v, Want: %v", err, expected)
		}
		return w
	})

	w2 := async.When(w1, func(interface{}) async.R {
		return async.NewErrorf("Expected not executed w1")
	})
	w3 := async.When(w2, func(_ interface{}, err error) async.R {
		if err != expected {
			return async.NewErrorf("Expected executed w.  Got: %v, Want: %v", err, expected)
		}
		return w2
	})

	w4 := async.When(w3, func() async.R {
		return async.NewErrorf("Expected not executed w3")
	})
	return async.When(w4, func(_ interface{}, err error) async.R {
		if err != expected {
			return async.NewErrorf("Expected executed w.  Got: %v, Want: %v", err, expected)
		}
		return async.Done()
	})
}
Beispiel #4
0
// RunActor starts a contained environment with a turn manager that runs to completion.
// main is the initial turn to be executed and the manager continues execution until main's return
// value is resolved.
func RunActor(root async.Func) error {
	done := make(chan error, 1)

	go func() {
		manager := turns.NewManager(turns.NewUniqueIDGenerator())
		runner := turns.NewTurnRunner(manager)
		tlsRelease := async.SetAmbientRunner(runner)
		defer tlsRelease()

		// Allocate a resolver to track the completion of the "main" function.
		r, s := async.NewR()

		// Queue to main routine for execution.
		manager.NewTurn("Main", func() {
			async.When(root(), func(err error) {
				log.Infof("Actor Completed with status: %v", err)

				s.Resolve(err)
			})
		})

		err := manager.RunUntil(r)
		done <- err
		close(done)
	}()

	err := <-done
	return err
}
Beispiel #5
0
// New implements async.Runner.New().
func (t *turnRunner) New(f async.Func) async.R {
	r, s := async.NewR()
	t.manager.NewTurn("New"+t.manager.NewID().String(), func() {
		next := f()
		s.Forward(next)
	})
	return r
}
// ResolveCoverage tests Resolver.Resolve().
func (t *TurnRunnerSuite) ResolveCoverage() async.R {
	r, s := async.NewR()
	async.When(async.Done(), func() {
		s.Resolve(nil)
	})

	expected := fmt.Errorf("some error")
	r2, s2 := async.NewR()
	async.When(r, func() {
		s2.Resolve(expected)
	})
	return async.When(r2, func(err error) error {
		if err != expected {
			return fmt.Errorf("Got: %v, Want: %v", err, expected)
		}
		return nil
	})
}
Beispiel #7
0
// NewCoverage provides coverage for the New function.
func (t *RSuite) NewCoverage() async.R {
	r, s := async.NewR()
	s.Complete()
	return async.When(r, func(val interface{}, err error) async.R {
		if err != nil {
			return async.NewErrorf("Expected successful completion.  Got: %v, Want: nil", err)
		}
		return async.Done()
	})
}
// NewResultForward tests Resolver.Forward().
func (t *TurnRunnerSuite) NewResultForward() async.R {
	expected := fmt.Errorf("some error")
	r, s := async.NewR()
	async.When(async.Done(), func() {
		s.Forward(async.NewError(expected))
	})
	return async.When(r, func(err error) error {
		if err != expected {
			return fmt.Errorf("Got: %v, Want: %v", err, expected)
		}
		return nil
	})
}
// NewResultForwarded tests a When on a forwarded result resolves correctly.
func (t *TurnRunnerSuite) NewResultForwarded() async.R {
	expected := fmt.Errorf("some error")
	r, s := async.NewR()

	// Forward the result synchronously causing the When to be directly enqueued on the error.
	s.Forward(async.NewError(expected))

	return async.When(r, func(err error) error {
		if err != expected {
			return fmt.Errorf("Got: %v, Want: %v", err, expected)
		}
		return nil
	})
}
Beispiel #10
0
// WhenCoverage provides coverage for the When function.
func (t *RSuite) WhenCoverage() async.R {
	w := async.When(async.Done(), func(val interface{}, err error) async.R {
		r, s := async.NewR()
		s.Complete()
		return r
	})

	// Verify full signature.
	expected := interface{}(nil)
	w1 := async.When(w, func(val interface{}, err error) async.R {
		if err != nil {
			return async.NewErrorf("Expected success w1.  Got: %v, Want: nil", err)
		}
		if val != expected {
			return async.NewErrorf("Expected value w1.  Got: %v, Want: %v", val, expected)
		}
		return w
	})

	// Verify just error.
	w2 := async.When(w1, func(err error) async.R {
		if err != nil {
			return async.NewErrorf("Expected success w2.  Got: %v, Want: nil", err)
		}
		return w1
	})

	// Verify just value.
	w3 := async.When(w2, func(val interface{}) async.R {
		if val != expected {
			return async.NewErrorf("Expected value w3.  Got: %v, Want: %v", val, expected)
		}
		return w2
	})

	// Verify neither value nor error.
	didRun := false
	w4 := async.When(w3, func() async.R {
		didRun = true
		return w3
	})
	return async.When(w4, func() async.R {
		if !didRun {
			return async.NewErrorf("Expected executed w4.  Got: %v, Want: true", didRun)
		}
		return w4
	})
}
Beispiel #11
0
// FinallyCoverage provides coverage for the Finally function.
func (t *RSuite) FinallyCoverage() async.R {
	expected := fmt.Errorf("some error")
	didRun := false
	r, s := async.NewR()
	s.Fail(expected)
	w := async.Finally(r, func() {
		didRun = true
	})

	// Verify that the value was round-tripped.
	return async.When(w, func(val interface{}, err error) async.R {
		if !didRun {
			return async.NewErrorf("Expected finally to run.  Got: false, Want: true")
		}
		if err != expected {
			return async.NewErrorf("Expected error.  Got: %v, Want: %v", err, expected)
		}
		if val != nil {
			return async.NewErrorf("Expected error.  Got: %v, Want: nil", val)
		}
		return async.Done()
	})
}
Beispiel #12
0
// WhenReturnCoverage provides coverage for the When function.
func (t *RSuite) WhenReturnCoverage() async.R {
	w := async.When(async.Done(), func() async.R {
		r, s := async.NewR()
		s.Complete()
		return r
	})

	w1 := async.When(w, func() (interface{}, error) {
		return nil, nil
	})

	w2 := async.When(w1, func() error {
		return nil
	})

	w3 := async.When(w2, func() interface{} {
		return nil
	})

	w4 := async.When(w3, func() {
	})

	return w4
}