pass_chan_select_multi.go raw

   1  package main
   2  
   3  import "moxie"
   4  
   5  // B.0.1 stress test: multi-channel select with bidirectional traffic.
   6  // Models the dispatch domain shape: select between an inbound work
   7  // channel and a result-channel from another spawned domain. Verifies:
   8  //   - select scales beyond 2 channels
   9  //   - polling cadence works with all channels idle then suddenly busy
  10  //   - sibling-frame buffering inside tryPipeRecv works under load
  11  
  12  func worker(in chan moxie.Int32, out chan moxie.Int32) {
  13  	for {
  14  		v, ok := <-in
  15  		if !ok {
  16  			break
  17  		}
  18  		out <- moxie.Int32(int32(v) * 2)
  19  	}
  20  }
  21  
  22  func main() {
  23  	in := make(chan moxie.Int32)
  24  	out := make(chan moxie.Int32)
  25  	spawn(worker, in, out)
  26  
  27  	const N = int32(200)
  28  	pending := int32(0)
  29  	sent := int32(0)
  30  	received := int32(0)
  31  	expectedSum := int32(0)
  32  	gotSum := int32(0)
  33  
  34  	// Send first value to prime the pipeline.
  35  	in <- moxie.Int32(0)
  36  	pending++
  37  	sent++
  38  
  39  	for received < N {
  40  		select {
  41  		case v := <-out:
  42  			gotSum += int32(v)
  43  			pending--
  44  			received++
  45  			if sent < N && pending < 4 {
  46  				in <- moxie.Int32(sent)
  47  				expectedSum += sent * 2
  48  				pending++
  49  				sent++
  50  			}
  51  		}
  52  	}
  53  	expectedSum += 0 * 2 // priming send
  54  	close(in)
  55  
  56  	if received != N || gotSum != expectedSum {
  57  		println("FAIL:select-multi", "rx", received, "sum", gotSum, "want", expectedSum)
  58  		return
  59  	}
  60  	println("PASS:select-multi")
  61  }
  62