Source file src/runtime/coro_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"internal/race"
     9  	"internal/testenv"
    10  	"runtime"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestCoroLockOSThread(t *testing.T) {
    16  	for _, test := range []string{
    17  		"CoroLockOSThreadIterLock",
    18  		"CoroLockOSThreadIterLockYield",
    19  		"CoroLockOSThreadLock",
    20  		"CoroLockOSThreadLockIterNested",
    21  		"CoroLockOSThreadLockIterLock",
    22  		"CoroLockOSThreadLockIterLockYield",
    23  		"CoroLockOSThreadLockIterYieldNewG",
    24  		"CoroLockOSThreadLockAfterPull",
    25  		"CoroLockOSThreadStopLocked",
    26  		"CoroLockOSThreadStopLockedIterNested",
    27  	} {
    28  		t.Run(test, func(t *testing.T) {
    29  			checkCoroTestProgOutput(t, runTestProg(t, "testprog", test))
    30  		})
    31  	}
    32  }
    33  
    34  func TestCoroCgoCallback(t *testing.T) {
    35  	testenv.MustHaveCGO(t)
    36  	if runtime.GOOS == "windows" {
    37  		t.Skip("coro cgo callback tests not supported on Windows")
    38  	}
    39  	if runtime.GOOS == "freebsd" && race.Enabled {
    40  		t.Skipf("race + cgo freebsd not supported. See https://go.dev/issue/73788.")
    41  	}
    42  	for _, test := range []string{
    43  		"CoroCgoIterCallback",
    44  		"CoroCgoIterCallbackYield",
    45  		"CoroCgoCallback",
    46  		"CoroCgoCallbackIterNested",
    47  		"CoroCgoCallbackIterCallback",
    48  		"CoroCgoCallbackIterCallbackYield",
    49  		"CoroCgoCallbackAfterPull",
    50  		"CoroCgoStopCallback",
    51  		"CoroCgoStopCallbackIterNested",
    52  	} {
    53  		t.Run(test, func(t *testing.T) {
    54  			checkCoroTestProgOutput(t, runTestProg(t, "testprogcgo", test))
    55  		})
    56  	}
    57  }
    58  
    59  func checkCoroTestProgOutput(t *testing.T, output string) {
    60  	t.Helper()
    61  
    62  	c := strings.SplitN(output, "\n", 2)
    63  	if len(c) == 1 {
    64  		t.Fatalf("expected at least one complete line in the output, got:\n%s", output)
    65  	}
    66  	expect, ok := strings.CutPrefix(c[0], "expect: ")
    67  	if !ok {
    68  		t.Fatalf("expected first line of output to start with \"expect: \", got: %q", c[0])
    69  	}
    70  	rest := c[1]
    71  	if expect == "OK" && rest != "OK\n" {
    72  		t.Fatalf("expected just 'OK' in the output, got:\n%s", rest)
    73  	}
    74  	if !strings.Contains(rest, expect) {
    75  		t.Fatalf("expected %q in the output, got:\n%s", expect, rest)
    76  	}
    77  }
    78  

View as plain text