golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/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  //go:build ignore
     6  
     7  //
     8  // This build tag means that "go build" does not build this file. Use "go run
     9  // manual_test_program.go" to run it.
    10  //
    11  // You will also need to change "debug = false" to "debug = true" in mmap_*.go.
    12  
    13  package main
    14  
    15  import (
    16  	"log"
    17  	"math/rand"
    18  	"time"
    19  
    20  	"golang.org/x/exp/mmap"
    21  )
    22  
    23  var garbage []byte
    24  
    25  func main() {
    26  	// If you replace "manual_test_program.go" with the name of an empty (zero
    27  	// sized) file (and set "const debug = true") then you will not necessarily
    28  	// see two "munmap log messages", since some operating systems will not
    29  	// allow a zero sized mapping so there is no need for a finalizer to unmap.
    30  	const filename = "manual_test_program.go"
    31  
    32  	for _, explicitClose := range []bool{false, true} {
    33  		r, err := mmap.Open(filename)
    34  		if err != nil {
    35  			log.Fatalf("Open: %v", err)
    36  		}
    37  		println("Open succeeded; Len =", r.Len())
    38  		if explicitClose {
    39  			r.Close()
    40  		} else {
    41  			// Leak the *mmap.ReaderAt returned by mmap.Open. The finalizer
    42  			// should pick it up, if finalizers run at all.
    43  		}
    44  	}
    45  
    46  	println("Finished all explicit Close calls.")
    47  	println("Creating and collecting garbage.")
    48  	println("Look for two munmap log messages.")
    49  	println("Hit Ctrl-C to exit.")
    50  
    51  	rng := rand.New(rand.NewSource(1))
    52  	now := time.Now()
    53  	for {
    54  		garbage = make([]byte, rng.Intn(1<<20))
    55  		if time.Since(now) > 1*time.Second {
    56  			now = time.Now()
    57  			print(".")
    58  		}
    59  	}
    60  }