github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/exp/mmap/manual_test_program.go (about)

     1  // Copyright 2015 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  // +build ignore
     6  //
     7  // This build tag means that "go build" does not build this file. Use "go run
     8  // manual_test_program.go" to run it.
     9  //
    10  // You will also need to change "debug = false" to "debug = true" in mmap_*.go.
    11  
    12  package main
    13  
    14  import (
    15  	"log"
    16  	"math/rand"
    17  	"time"
    18  
    19  	"golang.org/x/exp/mmap"
    20  )
    21  
    22  var garbage []byte
    23  
    24  func main() {
    25  	const filename = "manual_test_program.go"
    26  
    27  	for _, explicitClose := range []bool{false, true} {
    28  		r, err := mmap.Open(filename)
    29  		if err != nil {
    30  			log.Fatalf("Open: %v", err)
    31  		}
    32  		if explicitClose {
    33  			r.Close()
    34  		} else {
    35  			// Leak the *mmap.ReaderAt returned by mmap.Open. The finalizer
    36  			// should pick it up, if finalizers run at all.
    37  		}
    38  	}
    39  
    40  	println("Finished all explicit Close calls.")
    41  	println("Creating and collecting garbage.")
    42  	println("Look for two munmap log messages.")
    43  	println("Hit Ctrl-C to exit.")
    44  
    45  	rng := rand.New(rand.NewSource(1))
    46  	now := time.Now()
    47  	for {
    48  		garbage = make([]byte, rng.Intn(1<<20))
    49  		if time.Since(now) > 1*time.Second {
    50  			now = time.Now()
    51  			print(".")
    52  		}
    53  	}
    54  }