示例#1
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
}
func (t *TurnRunnerCoreSuite) Done() {
	log.Infof("Running test %s", base.GetMethodName())
	manager := NewManager(NewUniqueIDGenerator())
	runner := NewTurnRunner(manager)
	tlsRelease := async.SetAmbientRunner(runner)
	defer tlsRelease()

	done := async.Done()
	s := async.InternalUseOnlyGetResolver(done.ResultT).(*turnResolver)
	if !s.isResolved() {
		t.Errorf("Expected Done() to be resolved.  Got: %v, Want: resolved", done)
	}
}
func (t *TurnRunnerCoreSuite) OneTurn() {
	manager := NewManager(NewUniqueIDGenerator())
	runner := NewTurnRunner(manager)
	tlsRelease := async.SetAmbientRunner(runner)
	defer tlsRelease()

	done := make(chan struct{}, 0)
	async.New(func() async.R {
		close(done)
		return async.Done()
	})
	manager.runOneLoop()
	<-done
}