11package frankenphp_test
22
33import (
4+ "context"
45 "fmt"
56 "io"
67 "log"
@@ -15,6 +16,7 @@ import (
1516 "strings"
1617 "sync"
1718 "testing"
19+ "time"
1820
1921 "github.com/dunglas/frankenphp"
2022 "github.com/stretchr/testify/assert"
@@ -388,6 +390,102 @@ func testLog(t *testing.T, opts *testOptions) {
388390 }, opts )
389391}
390392
393+ func TestConnectionAbortNormal_module (t * testing.T ) { testConnectionAbortNormal (t , & testOptions {}) }
394+ func TestConnectionAbortNormal_worker (t * testing.T ) {
395+ testConnectionAbortNormal (t , & testOptions {workerScript : "connectionStatusLog.php" })
396+ }
397+ func testConnectionAbortNormal (t * testing.T , opts * testOptions ) {
398+ logger , logs := observer .New (zap .InfoLevel )
399+ opts .logger = zap .New (logger )
400+
401+ runTest (t , func (handler func (http.ResponseWriter , * http.Request ), _ * httptest.Server , i int ) {
402+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/connectionStatusLog.php?i=%d" , i ), nil )
403+ w := httptest .NewRecorder ()
404+
405+ ctx , cancel := context .WithCancel (req .Context ())
406+ req = req .WithContext (ctx )
407+ cancel ()
408+ handler (w , req )
409+
410+ // todo: remove conditions on wall clock to avoid race conditions/flakiness
411+ time .Sleep (1000 * time .Microsecond )
412+ var found bool
413+ searched := fmt .Sprintf ("request %d: 1" , i )
414+ for _ , entry := range logs .All () {
415+ if entry .Message == searched {
416+ found = true
417+ break
418+ }
419+ }
420+
421+ assert .True (t , found )
422+ }, opts )
423+ }
424+
425+ func TestConnectionAbortFlush_module (t * testing.T ) { testConnectionAbortFlush (t , & testOptions {}) }
426+ func TestConnectionAbortFlush_worker (t * testing.T ) {
427+ testConnectionAbortFlush (t , & testOptions {workerScript : "connectionStatusLog.php" })
428+ }
429+ func testConnectionAbortFlush (t * testing.T , opts * testOptions ) {
430+ logger , logs := observer .New (zap .InfoLevel )
431+ opts .logger = zap .New (logger )
432+
433+ runTest (t , func (handler func (w http.ResponseWriter , response * http.Request ), _ * httptest.Server , i int ) {
434+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/connectionStatusLog.php?i=%d&flush" , i ), nil )
435+ w := httptest .NewRecorder ()
436+
437+ ctx , cancel := context .WithCancel (req .Context ())
438+ req = req .WithContext (ctx )
439+ cancel ()
440+ handler (w , req )
441+
442+ // todo: remove conditions on wall clock to avoid race conditions/flakiness
443+ time .Sleep (1000 * time .Microsecond )
444+ var found bool
445+ searched := fmt .Sprintf ("request %d: 1" , i )
446+ for _ , entry := range logs .All () {
447+ if entry .Message == searched {
448+ found = true
449+ break
450+ }
451+ }
452+
453+ assert .True (t , found )
454+ }, opts )
455+ }
456+
457+ func TestConnectionAbortFinish_module (t * testing.T ) { testConnectionAbortFinish (t , & testOptions {}) }
458+ func TestConnectionAbortFinish_worker (t * testing.T ) {
459+ testConnectionAbortFinish (t , & testOptions {workerScript : "connectionStatusLog.php" })
460+ }
461+ func testConnectionAbortFinish (t * testing.T , opts * testOptions ) {
462+ logger , logs := observer .New (zap .InfoLevel )
463+ opts .logger = zap .New (logger )
464+
465+ runTest (t , func (handler func (w http.ResponseWriter , response * http.Request ), _ * httptest.Server , i int ) {
466+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("http://example.com/connectionStatusLog.php?i=%d&finish" , i ), nil )
467+ w := httptest .NewRecorder ()
468+
469+ ctx , cancel := context .WithCancel (req .Context ())
470+ req = req .WithContext (ctx )
471+ cancel ()
472+ handler (w , req )
473+
474+ // todo: remove conditions on wall clock to avoid race conditions/flakiness
475+ time .Sleep (1000 * time .Microsecond )
476+ var found bool
477+ searched := fmt .Sprintf ("request %d: 0" , i )
478+ for _ , entry := range logs .All () {
479+ if entry .Message == searched {
480+ found = true
481+ break
482+ }
483+ }
484+
485+ assert .True (t , found )
486+ }, opts )
487+ }
488+
391489func TestException_module (t * testing.T ) { testException (t , & testOptions {}) }
392490func TestException_worker (t * testing.T ) {
393491 testException (t , & testOptions {workerScript : "exception.php" })
0 commit comments