github.com/vvnotw/moby@v1.13.1/pkg/plugins/plugin_test.go (about)

     1  package plugins
     2  
     3  import (
     4  	"errors"
     5  	"path/filepath"
     6  	"runtime"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // regression test for deadlock in handlers
    13  func TestPluginAddHandler(t *testing.T) {
    14  	// make a plugin which is pre-activated
    15  	p := &Plugin{activateWait: sync.NewCond(&sync.Mutex{})}
    16  	p.Manifest = &Manifest{Implements: []string{"bananas"}}
    17  	storage.plugins["qwerty"] = p
    18  
    19  	testActive(t, p)
    20  	Handle("bananas", func(_ string, _ *Client) {})
    21  	testActive(t, p)
    22  }
    23  
    24  func TestPluginWaitBadPlugin(t *testing.T) {
    25  	p := &Plugin{activateWait: sync.NewCond(&sync.Mutex{})}
    26  	p.activateErr = errors.New("some junk happened")
    27  	testActive(t, p)
    28  }
    29  
    30  func testActive(t *testing.T, p *Plugin) {
    31  	done := make(chan struct{})
    32  	go func() {
    33  		p.waitActive()
    34  		close(done)
    35  	}()
    36  
    37  	select {
    38  	case <-time.After(100 * time.Millisecond):
    39  		_, f, l, _ := runtime.Caller(1)
    40  		t.Fatalf("%s:%d: deadlock in waitActive", filepath.Base(f), l)
    41  	case <-done:
    42  	}
    43  
    44  }