github.com/containers/podman/v4@v4.9.4/pkg/bindings/test/connection_test.go (about)

     1  package bindings_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/containers/podman/v4/pkg/bindings/containers"
     8  	"github.com/containers/podman/v4/pkg/bindings/system"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman connection", func() {
    15  	var (
    16  		bt *bindingTest
    17  		s  *gexec.Session
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		bt = newBindingTest()
    22  		bt.RestoreImagesFromCache()
    23  		s = bt.startAPIService()
    24  		time.Sleep(1 * time.Second)
    25  		err := bt.NewConnection()
    26  		Expect(err).ToNot(HaveOccurred())
    27  	})
    28  
    29  	AfterEach(func() {
    30  		s.Kill()
    31  		bt.cleanup()
    32  	})
    33  
    34  	It("request on cancelled context results in error", func() {
    35  		ctx, cancel := context.WithCancel(bt.conn)
    36  		cancel()
    37  		_, err := system.Version(ctx, nil)
    38  		Expect(err).To(MatchError(ctx.Err()))
    39  	})
    40  
    41  	It("cancel request in flight reports cancelled context", func() {
    42  		var name = "top"
    43  		_, err := bt.RunTopContainer(&name, nil)
    44  		Expect(err).ToNot(HaveOccurred())
    45  
    46  		errChan := make(chan error)
    47  		ctx, cancel := context.WithCancel(bt.conn)
    48  
    49  		go func() {
    50  			defer close(errChan)
    51  			_, err := containers.Wait(ctx, name, nil)
    52  			errChan <- err
    53  		}()
    54  
    55  		// Wait for the goroutine to fire the request
    56  		time.Sleep(1 * time.Second)
    57  
    58  		cancel()
    59  
    60  		select {
    61  		case err, ok := <-errChan:
    62  			Expect(ok).To(BeTrue())
    63  			Expect(err).To(MatchError(ctx.Err()))
    64  		case <-time.NewTimer(1 * time.Second).C:
    65  			Fail("cancelled request did not return in less than 1 second")
    66  		}
    67  	})
    68  })