Source file test/fixedbugs/issue15281.go

     1  // run
     2  
     3  // Copyright 2016 The Go Authors.  All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import "runtime"
    10  
    11  func main() {
    12  	{
    13  		x := inuse()
    14  		c := make(chan []byte, 10)
    15  		c <- make([]byte, 10<<20)
    16  		close(c)
    17  		f1(c, x)
    18  	}
    19  	{
    20  		x := inuse()
    21  		c := make(chan []byte, 10)
    22  		c <- make([]byte, 10<<20)
    23  		close(c)
    24  		f2(c, x)
    25  	}
    26  }
    27  
    28  func f1(c chan []byte, start int64) {
    29  	for x := range c {
    30  		if delta := inuse() - start; delta < 9<<20 {
    31  			println("BUG: f1: after alloc: expected delta at least 9MB, got: ", delta)
    32  			println(x)
    33  		}
    34  		x = nil
    35  		if delta := inuse() - start; delta > 1<<20 {
    36  			println("BUG: f1: after alloc: expected delta below 1MB, got: ", delta)
    37  			println(x)
    38  		}
    39  	}
    40  }
    41  
    42  func f2(c chan []byte, start int64) {
    43  	for {
    44  		x, ok := <-c
    45  		if !ok {
    46  			break
    47  		}
    48  		if delta := inuse() - start; delta < 9<<20 {
    49  			println("BUG: f2: after alloc: expected delta at least 9MB, got: ", delta)
    50  			println(x)
    51  		}
    52  		x = nil
    53  		if delta := inuse() - start; delta > 1<<20 {
    54  			println("BUG: f2: after alloc: expected delta below 1MB, got: ", delta)
    55  			println(x)
    56  		}
    57  	}
    58  }
    59  
    60  func inuse() int64 {
    61  	runtime.GC()
    62  	var st runtime.MemStats
    63  	runtime.ReadMemStats(&st)
    64  	return int64(st.Alloc)
    65  }
    66  

View as plain text