func TestReallocate(t *testing.T) {
	_, storage, backing, si, destroyFunc := newStorage(t)
	defer destroyFunc()
	if err := si.Create(context.TODO(), key(), validNewRangeAllocation(), nil, 0); err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// Allocate a port inside the valid port range
	if err := storage.Allocate(30100); err != nil {
		t.Fatal(err)
	}

	// Try to allocate the same port in the local bitmap
	// The local bitmap stores the offset of the port
	// offset = port - base (30100 - 30000 = 100)
	ok, err := backing.Allocate(100)
	if err != nil {
		t.Fatal(err)
	}
	// It should not allocate the port because it was already allocated
	if ok {
		t.Fatal("Expected allocation to fail")
	}
	// Try to allocate the port again should fail
	if err := storage.Allocate(30100); err != portallocator.ErrAllocated {
		t.Fatal(err)
	}

	// Release the port
	if err := storage.Release(30100); err != nil {
		t.Fatal(err)
	}

	// Try to allocate the port again should succeed because we've released it
	if err := storage.Allocate(30100); err != nil {
		t.Fatal(err)
	}

}
// TestReallocate test that we can not allocate a port already allocated until it is released
TestReallocate test that we can not allocate a port already allocated until it is released
// TestReallocate test that we can not allocate a port already allocated until it is released
func TestReallocate(t *testing.T) {
	_, storage, backing, si, destroyFunc := newStorage(t)
	defer destroyFunc()
	if err := si.Create(context.TODO(), key(), validNewRangeAllocation(), nil, 0); err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if err := storage.Allocate(30100); err != nil {
		t.Fatal(err)
	}
	ok, err := backing.Allocate(100)
	if err != nil {
		t.Fatal(err)
	}
	if ok {
		t.Fatal("Expected allocation to fail")
	}
	if err := storage.Allocate(30100); err != portallocator.ErrAllocated {
		t.Fatal(err)
	}
	if err := storage.Release(30100); err != nil {
		t.Fatal(err)
	}
	if err := storage.Allocate(30100); err != nil {
		t.Fatal(err)
	}
}
func TestReallocate ( t * testing . T ) { _ , storage , backing , si , destroyFunc := newStorage ( t ) defer destroyFunc ( ) if err := si . Create ( context . TODO ( ) , key ( ) , validNewRangeAllocation ( ) , nil , 0 ) ; err != nil { t . Fatalf ( " " , err ) } if err := storage . Allocate ( 30100 ) ; err != nil { t . Fatal ( err ) } ok , err := backing . Allocate ( 100 ) if err != nil { t . Fatal ( err ) } if ok { t . Fatal ( " " ) } if err := storage . Allocate ( 30100 ) ; err != portallocator . ErrAllocated { t . Fatal ( err ) } if err := storage . Release ( 30100 ) ; err != nil { t . Fatal ( err ) } if err := storage . Allocate ( 30100 ) ; err != nil { t . Fatal ( err ) } }
TestReallocate test that we can not allocate a port already allocated until it is released
TestReallocate test that we can not allocate a port already allocated until it is released
// Allocate a port inside the valid port range // Try to allocate the same port in the local bitmap // The local bitmap stores the offset of the port // offset = port - base (30100 - 30000 = 100) // It should not allocate the port because it was already allocated // Try to allocate the port again should fail // Release the port // Try to allocate the port again should succeed because we've released it