thread_state_test.go 973 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package frankenphp
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "go.uber.org/zap"
  7. )
  8. func TestYieldToEachOtherViaThreadStates(t *testing.T) {
  9. threadState := &threadStateHandler{currentState: stateBooting}
  10. go func() {
  11. threadState.waitFor(stateInactive)
  12. assert.True(t, threadState.is(stateInactive))
  13. threadState.set(stateActive)
  14. }()
  15. threadState.set(stateInactive)
  16. threadState.waitFor(stateActive)
  17. assert.True(t, threadState.is(stateActive))
  18. }
  19. func TestYieldToAWaitGroupPassedByThreadState(t *testing.T) {
  20. logger, _ = zap.NewDevelopment()
  21. threadState := &threadStateHandler{currentState: stateBooting}
  22. hasYielded := false
  23. wg := sync.WaitGroup{}
  24. wg.Add(1)
  25. go func() {
  26. threadState.set(stateInactive)
  27. threadState.waitForAndYield(&wg, stateActive)
  28. hasYielded = true
  29. wg.Done()
  30. }()
  31. threadState.waitFor(stateInactive)
  32. threadState.set(stateActive)
  33. // 'set' should have yielded to the wait group
  34. assert.True(t, hasYielded)
  35. }