github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/heartbeatstop_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/helper/uuid"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/hashicorp/nomad/testutil"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestHeartbeatStop_allocHook(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	server, _, cleanupS1 := testServer(t, nil)
    18  	defer cleanupS1()
    19  	testutil.WaitForLeader(t, server.RPC)
    20  
    21  	client, cleanupC1 := TestClient(t, func(c *config.Config) {
    22  		c.RPCHandler = server
    23  	})
    24  	defer cleanupC1()
    25  
    26  	// an allocation, with a tiny lease
    27  	d := 1 * time.Microsecond
    28  	alloc := &structs.Allocation{
    29  		ID:        uuid.Generate(),
    30  		TaskGroup: "foo",
    31  		Job: &structs.Job{
    32  			TaskGroups: []*structs.TaskGroup{
    33  				{
    34  					Name:                      "foo",
    35  					StopAfterClientDisconnect: &d,
    36  				},
    37  			},
    38  		},
    39  		Resources: &structs.Resources{
    40  			CPU:      100,
    41  			MemoryMB: 100,
    42  			DiskMB:   0,
    43  		},
    44  	}
    45  
    46  	// alloc added to heartbeatStop.allocs
    47  	err := client.addAlloc(alloc, "")
    48  	require.NoError(t, err)
    49  	testutil.WaitForResult(func() (bool, error) {
    50  		_, ok := client.heartbeatStop.allocInterval[alloc.ID]
    51  		return ok, nil
    52  	}, func(err error) {
    53  		require.NoError(t, err)
    54  	})
    55  
    56  	// the tiny lease causes the watch loop to destroy it
    57  	testutil.WaitForResult(func() (bool, error) {
    58  		_, ok := client.heartbeatStop.allocInterval[alloc.ID]
    59  		return !ok, nil
    60  	}, func(err error) {
    61  		require.NoError(t, err)
    62  	})
    63  
    64  	require.Empty(t, client.allocs[alloc.ID])
    65  }