File: cdata_test.go

package info (click to toggle)
ciborium 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,928 kB
  • sloc: cpp: 2,199; sh: 75; makefile: 20
file content (42 lines) | stat: -rw-r--r-- 714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cdata

import (
	"runtime"
	"sync"
	"testing"
)

type refPair struct {
	ref1, ref2 int64
}

func TestRef(t *testing.T) {
	const N = 10
	runtime.LockOSThread()
	exit := sync.WaitGroup{}
	exit.Add(1)
	defer exit.Done()
	wg := sync.WaitGroup{}
	wg.Add(N)
	ch := make(chan refPair)
	for i := 0; i < N; i++ {
		go func() {
			runtime.LockOSThread()
			wg.Done()
			ch <- refPair{Ref(), Ref()}
			exit.Wait()
		}()
	}
	wg.Wait()
	refs := make(map[int64]bool)
	for i := 0; i < N; i++ {
		pair := <-ch
		if pair.ref1 != pair.ref2 {
			t.Fatalf("found inconsistent ref: %d != %d", pair.ref1, pair.ref2)
		}
		if refs[pair.ref1] {
			t.Fatalf("found duplicated ref: %d", pair.ref1)
		}
		refs[pair.ref1] = true
	}
}