Skip to content

Commit 0e8f837

Browse files
committed
all: add test on linux
1 parent f82d015 commit 0e8f837

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

.github/workflows/mainthread.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ name: mainthread
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ main ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ main ]
88

99
jobs:
10-
1110
build:
1211
name: Build
1312
runs-on: ubuntu-latest
@@ -28,8 +27,3 @@ jobs:
2827
- name: Test
2928
run: |
3029
go test -v -coverprofile=coverage.txt -covermode=atomic ./...
31-
- name: Upload coverage profile
32-
uses: codecov/codecov-action@v1
33-
with:
34-
token: ${{secrets.CODECOV_TOKEN}}
35-
file: coverage.txt

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# mainthread
22

33
[![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/mainthread)](https://pkg.go.dev/golang.design/x/mainthread) [![Go Report Card](https://goreportcard.com/badge/golang.design/x/mainthread)](https://goreportcard.com/report/golang.design/x/mainthread)
4-
![mainthread](https://github.com/golang-design/mainthread/workflows/mainthread/badge.svg?branch=master)
4+
![mainthread](https://github.com/golang-design/mainthread/workflows/mainthread/badge.svg?branch=main)
55

66
Package mainthread schedules function calls on the main thread in Go.
77

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module golang.design/x/mainthread
22

33
go 1.15
4+
5+
require golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd h1:WgqgiQvkiZWz7XLhphjt2GI2GcGCTIZs9jqXMWmH+oc=
2+
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

mainthread_test.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,60 @@ package mainthread_test
66

77
import (
88
"fmt"
9+
"os"
10+
"sync"
11+
"sync/atomic"
912
"testing"
1013

1114
"golang.design/x/mainthread"
15+
"golang.org/x/sys/unix"
1216
)
1317

14-
func TestMainthread(t *testing.T) {
18+
var initTid int
19+
20+
func init() {
21+
initTid = unix.Getpid()
22+
}
23+
24+
func TestMain(m *testing.M) {
1525
mainthread.Init(func() {
16-
mainthread.Call(func() {
17-
// FIXME: how to test this function really runs on the main thread?
18-
})
26+
os.Exit(m.Run())
1927
})
2028
}
2129

30+
func TestMainThread(t *testing.T) {
31+
var (
32+
nummain uint64
33+
numcall = 100000
34+
)
35+
36+
wg := sync.WaitGroup{}
37+
for i := 0; i < numcall; i++ {
38+
wg.Add(2)
39+
go func() {
40+
defer wg.Done()
41+
mainthread.Call(func() {
42+
tid := unix.Gettid()
43+
if tid == initTid {
44+
return
45+
}
46+
t.Fatalf("call is not executed on the main thread, want %d, got %d", initTid, tid)
47+
})
48+
}()
49+
go func() {
50+
defer wg.Done()
51+
if unix.Gettid() == initTid {
52+
atomic.AddUint64(&nummain, 1)
53+
}
54+
}()
55+
}
56+
wg.Wait()
57+
58+
if nummain == uint64(numcall) {
59+
t.Fatalf("all non main thread calls are executed on the main thread.")
60+
}
61+
}
62+
2263
func BenchmarkCall(b *testing.B) {
2364
f1 := func() {}
2465
f2 := func() {}

0 commit comments

Comments
 (0)