gorgonia.org/gorgonia@v0.9.17/x/vm/example_node_test.go (about)

     1  package xvm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"gorgonia.org/gorgonia"
     9  )
    10  
    11  func Examplenode_Compute() {
    12  	forty := gorgonia.F32(40.0)
    13  	two := gorgonia.F32(2.0)
    14  	n := &node{
    15  		op:          &sumF32{},
    16  		inputValues: make([]gorgonia.Value, 2),
    17  		outputC:     make(chan gorgonia.Value, 0),
    18  		inputC:      make(chan ioValue, 0),
    19  	}
    20  	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
    21  	// releases resources if ComputeForward completes before timeout elapses
    22  	defer cancel()
    23  
    24  	go n.Compute(ctx)
    25  	n.inputC <- struct {
    26  		pos int
    27  		v   gorgonia.Value
    28  	}{
    29  		0,
    30  		&forty,
    31  	}
    32  	n.inputC <- struct {
    33  		pos int
    34  		v   gorgonia.Value
    35  	}{
    36  		1,
    37  		&two,
    38  	}
    39  	fmt.Println(<-n.outputC)
    40  	// output: 42
    41  }