Skip to content

Commit

Permalink
Adds state tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alliballibaba2 committed Dec 7, 2024
1 parent cca2a00 commit ec8aeb7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 44 deletions.
File renamed without changes.
48 changes: 48 additions & 0 deletions state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package frankenphp

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func Test2GoroutinesYieldToEachOtherViaStates(t *testing.T) {
threadState := &threadState{currentState: stateBooting}

go func() {
threadState.waitFor(stateInactive)
assert.True(t, threadState.is(stateInactive))
threadState.set(stateReady)
}()

threadState.set(stateInactive)
threadState.waitFor(stateReady)
assert.True(t, threadState.is(stateReady))
}

func TestStateShouldHaveCorrectAmountOfSubscribers(t *testing.T) {
threadState := &threadState{currentState: stateBooting}

// 3 subscribers waiting for different states
go threadState.waitFor(stateInactive)
go threadState.waitFor(stateInactive, stateShuttingDown)
go threadState.waitFor(stateShuttingDown)

time.Sleep(1 * time.Millisecond)
assertNumberOfSubscribers(t, threadState, 3)

threadState.set(stateInactive)
time.Sleep(1 * time.Millisecond)
assertNumberOfSubscribers(t, threadState, 1)

threadState.set(stateShuttingDown)
time.Sleep(1 * time.Millisecond)
assertNumberOfSubscribers(t, threadState, 0)
}

func assertNumberOfSubscribers(t *testing.T, threadState *threadState, expected int) {
threadState.mu.RLock()
assert.Len(t, threadState.subscribers, expected)
threadState.mu.RUnlock()
}
21 changes: 0 additions & 21 deletions thread-state_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions thread-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func convertToWorkerThread(thread *phpThread, worker *worker) {
maxConsecutiveFailures: 6,
},
})
worker.addThread(thread)
worker.attachThread(thread)
if worker.fileName == "" {
panic("worker script is empty")
}
Expand All @@ -54,7 +54,7 @@ func (handler *workerThread) beforeScriptExecution() string {
switch handler.state.get() {
case stateTransitionRequested:
thread := handler.thread
handler.worker.removeThread(handler.thread)
handler.worker.detachThread(handler.thread)
thread.state.set(stateTransitionInProgress)
thread.state.waitFor(stateTransitionComplete, stateShuttingDown)

Expand Down
38 changes: 17 additions & 21 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func initWorkers(opt []workerOpt) error {
return err
}
for i := 0; i < worker.num; i++ {
worker.startNewThread()
thread := getInactivePHPThread()
convertToWorkerThread(thread, worker)
}
}

Expand Down Expand Up @@ -112,9 +113,21 @@ func getDirectoriesToWatch(workerOpts []workerOpt) []string {
return directoriesToWatch
}

func (worker *worker) startNewThread() {
thread := getInactivePHPThread()
convertToWorkerThread(thread, worker)
func (worker *worker) attachThread(thread *phpThread) {
worker.threadMutex.Lock()
worker.threads = append(worker.threads, thread)
worker.threadMutex.Unlock()
}

func (worker *worker) detachThread(thread *phpThread) {
worker.threadMutex.Lock()
for i, t := range worker.threads {
if t == thread {
worker.threads = append(worker.threads[:i], worker.threads[i+1:]...)
break
}
}
worker.threadMutex.Unlock()
}

func (worker *worker) handleRequest(r *http.Request, fc *FrankenPHPContext) {
Expand All @@ -140,20 +153,3 @@ func (worker *worker) handleRequest(r *http.Request, fc *FrankenPHPContext) {
<-fc.done
metrics.StopWorkerRequest(worker.fileName, time.Since(fc.startedAt))
}

func (worker *worker) addThread(thread *phpThread) {
worker.threadMutex.Lock()
worker.threads = append(worker.threads, thread)
worker.threadMutex.Unlock()
}

func (worker *worker) removeThread(thread *phpThread) {
worker.threadMutex.Lock()
for i, t := range worker.threads {
if t == thread {
worker.threads = append(worker.threads[:i], worker.threads[i+1:]...)
break
}
}
worker.threadMutex.Unlock()
}

0 comments on commit ec8aeb7

Please sign in to comment.