github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/cmd/badfuncs/main.go (about)

     1  // Copyright 2019 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // Badfuncs is a binary that tests various scenarios of Func creation that may
     6  // fail to satisfy the invariant that all workers share common definitions of
     7  // Funcs. All tests should result in a panic except for 'ok'.
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"flag"
    13  	"fmt"
    14  	"math/rand"
    15  	"os"
    16  	"time"
    17  
    18  	"github.com/grailbio/bigslice"
    19  	"github.com/grailbio/bigslice/sliceconfig"
    20  )
    21  
    22  var makeFuncs = []func() *bigslice.FuncValue{
    23  	func() *bigslice.FuncValue {
    24  		return bigslice.Func(func() bigslice.Slice {
    25  			return bigslice.Const(4, []int{})
    26  		})
    27  	},
    28  	func() *bigslice.FuncValue {
    29  		return bigslice.Func(func() bigslice.Slice {
    30  			return bigslice.Const(4, []int{})
    31  		})
    32  	},
    33  	func() *bigslice.FuncValue {
    34  		return bigslice.Func(func() bigslice.Slice {
    35  			return bigslice.Const(4, []int{})
    36  		})
    37  	},
    38  	func() *bigslice.FuncValue {
    39  		return bigslice.Func(func() bigslice.Slice {
    40  			return bigslice.Const(4, []int{})
    41  		})
    42  	},
    43  }
    44  
    45  func ok() {
    46  	funcs := make([]*bigslice.FuncValue, len(makeFuncs))
    47  	for i, makeFunc := range makeFuncs {
    48  		funcs[i] = makeFunc()
    49  	}
    50  	sess := sliceconfig.Parse()
    51  	defer sess.Shutdown()
    52  
    53  	ctx := context.Background()
    54  	sess.Must(ctx, funcs[0])
    55  }
    56  
    57  func toolate() {
    58  	sess := sliceconfig.Parse()
    59  	defer sess.Shutdown()
    60  
    61  	f0 := makeFuncs[0]()
    62  	ctx := context.Background()
    63  	sess.Must(ctx, f0)
    64  }
    65  
    66  func random() {
    67  	rand.Seed(time.Now().UTC().UnixNano())
    68  	rand.Shuffle(len(makeFuncs), func(i, j int) {
    69  		makeFuncs[i], makeFuncs[j] = makeFuncs[j], makeFuncs[i]
    70  	})
    71  	funcs := make([]*bigslice.FuncValue, len(makeFuncs))
    72  	for i, makeFunc := range makeFuncs {
    73  		funcs[i] = makeFunc()
    74  	}
    75  	sess := sliceconfig.Parse()
    76  	defer sess.Shutdown()
    77  	ctx := context.Background()
    78  	sess.Must(ctx, funcs[0])
    79  }
    80  
    81  func main() {
    82  	flag.Usage = func() {
    83  		fmt.Fprintf(os.Stderr, `usage: badfuncstest-name
    84  
    85  Command badfuncs tests various scenarios of Func creation that may fail to
    86  satisfy the invariant that all workers share common definitions of Funcs. All
    87  tests should result in a panic except for 'ok'.
    88  
    89  Available tests are:
    90  
    91  	ok
    92  		Funcs are properly created.
    93  	toolate
    94  		Funcs are created after exec.Start, so they are not available on
    95  		workers.
    96  	random
    97  		Funcs are created in random order. (Note that this may not panic if all
    98  		workers randomly produce the same funcs).
    99  
   100  `)
   101  		flag.PrintDefaults()
   102  		os.Exit(2)
   103  	}
   104  	if len(os.Args) < 2 {
   105  		flag.Usage()
   106  	}
   107  	cmd := os.Args[len(os.Args)-1]
   108  	switch cmd {
   109  	case "ok":
   110  		ok()
   111  	case "toolate":
   112  		toolate()
   113  	case "random":
   114  		random()
   115  	default:
   116  		fmt.Fprintf(os.Stderr, "unknown test %s\n", cmd)
   117  		flag.Usage()
   118  	}
   119  }