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/testenv"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestCoroLockOSThread(t *testing.T) {
    15  	for _, test := range []string{
    16  		"CoroLockOSThreadIterLock",
    17  		"CoroLockOSThreadIterLockYield",
    18  		"CoroLockOSThreadLock",
    19  		"CoroLockOSThreadLockIterNested",
    20  		"CoroLockOSThreadLockIterLock",
    21  		"CoroLockOSThreadLockIterLockYield",
    22  		"CoroLockOSThreadLockIterYieldNewG",
    23  		"CoroLockOSThreadLockAfterPull",
    24  		"CoroLockOSThreadStopLocked",
    25  		"CoroLockOSThreadStopLockedIterNested",
    26  	} {
    27  		t.Run(test, func(t *testing.T) {
    28  			checkCoroTestProgOutput(t, runTestProg(t, "testprog", test))
    29  		})
    30  	}
    31  }
    32  
    33  func TestCoroCgoCallback(t *testing.T) {
    34  	testenv.MustHaveCGO(t)
    35  	if runtime.GOOS == "windows" {
    36  		t.Skip("coro cgo callback tests not supported on Windows")
    37  	}
    38  	for _, test := range []string{
    39  		"CoroCgoIterCallback",
    40  		"CoroCgoIterCallbackYield",
    41  		"CoroCgoCallback",
    42  		"CoroCgoCallbackIterNested",
    43  		"CoroCgoCallbackIterCallback",
    44  		"CoroCgoCallbackIterCallbackYield",
    45  		"CoroCgoCallbackAfterPull",
    46  		"CoroCgoStopCallback",
    47  		"CoroCgoStopCallbackIterNested",
    48  	} {
    49  		t.Run(test, func(t *testing.T) {
    50  			checkCoroTestProgOutput(t, runTestProg(t, "testprogcgo", test))
    51  		})
    52  	}
    53  }
    54  
    55  func checkCoroTestProgOutput(t *testing.T, output string) {
    56  	t.Helper()
    57  
    58  	c := strings.SplitN(output, "\n", 2)
    59  	if len(c) == 1 {
    60  		t.Fatalf("expected at least one complete line in the output, got:\n%s", output)
    61  	}
    62  	expect, ok := strings.CutPrefix(c[0], "expect: ")
    63  	if !ok {
    64  		t.Fatalf("expected first line of output to start with \"expect: \", got: %q", c[0])
    65  	}
    66  	rest := c[1]
    67  	if expect == "OK" && rest != "OK\n" {
    68  		t.Fatalf("expected just 'OK' in the output, got:\n%s", rest)
    69  	}
    70  	if !strings.Contains(rest, expect) {
    71  		t.Fatalf("expected %q in the output, got:\n%s", expect, rest)
    72  	}
    73  }
    74  

View as plain text