]> Cypherpunks repositories - gostls13.git/commitdiff
internal/fmtsort: order channels in test in memory address order
authorKeith Randall <khr@golang.org>
Mon, 8 Nov 2021 05:29:30 +0000 (21:29 -0800)
committerKeith Randall <khr@golang.org>
Mon, 8 Nov 2021 17:30:10 +0000 (17:30 +0000)
Kind of a kludge, but it makes the test work reliably.

Fixes #49431

Change-Id: Ic2a075ba02f80ea7efcc1b3f0f5a43649e87c0d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/361918
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/internal/fmtsort/sort_test.go

index 5c4db1c5faa950236eba33bac73263d304299b7a..ab063af5bae660d59ea165715f32c913e62a7b14 100644 (file)
@@ -9,6 +9,7 @@ import (
        "internal/fmtsort"
        "math"
        "reflect"
+       "sort"
        "strings"
        "testing"
        "unsafe"
@@ -188,9 +189,19 @@ func sprintKey(key reflect.Value) string {
 
 var (
        ints  [3]int
-       chans = [3]chan int{make(chan int), make(chan int), make(chan int)}
+       chans = makeChans()
 )
 
+func makeChans() []chan int {
+       cs := []chan int{make(chan int), make(chan int), make(chan int)}
+       // Order channels by address. See issue #49431.
+       // TODO: pin these pointers once pinning is available (#46787).
+       sort.Slice(cs, func(i, j int) bool {
+               return uintptr(reflect.ValueOf(cs[i]).UnsafePointer()) < uintptr(reflect.ValueOf(cs[j]).UnsafePointer())
+       })
+       return cs
+}
+
 func pointerMap() map[*int]string {
        m := make(map[*int]string)
        for i := 2; i >= 0; i-- {