mirror of
https://github.com/status-im/consul.git
synced 2025-02-13 14:16:35 +00:00
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package consul
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type mockServerMetadataWriter struct {
|
|
writeErr error
|
|
}
|
|
|
|
func (m *mockServerMetadataWriter) Write(p []byte) (n int, err error) {
|
|
if m.writeErr != nil {
|
|
return 0, m.writeErr
|
|
}
|
|
|
|
return 1, nil
|
|
}
|
|
|
|
func TestServerMetadata(t *testing.T) {
|
|
now := time.Now()
|
|
|
|
t.Run("TestIsLastSeenStaleTrue", func(t *testing.T) {
|
|
// Create a server that is 48 hours old.
|
|
md := &ServerMetadata{
|
|
LastSeenUnix: now.Add(-48 * time.Hour).Unix(),
|
|
}
|
|
|
|
stale := md.IsLastSeenStale(24 * time.Hour)
|
|
assert.True(t, stale)
|
|
})
|
|
|
|
t.Run("TestIsLastSeenStaleFalse", func(t *testing.T) {
|
|
// Create a server that is 1 hour old.
|
|
md := &ServerMetadata{
|
|
LastSeenUnix: now.Add(-1 * time.Hour).Unix(),
|
|
}
|
|
|
|
stale := md.IsLastSeenStale(24 * time.Hour)
|
|
assert.False(t, stale)
|
|
})
|
|
}
|
|
|
|
func TestWriteServerMetadata(t *testing.T) {
|
|
t.Run("TestWriteError", func(t *testing.T) {
|
|
m := &mockServerMetadataWriter{
|
|
writeErr: errors.New("write error"),
|
|
}
|
|
|
|
err := WriteServerMetadata(m)
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
t.Run("TestOK", func(t *testing.T) {
|
|
b := new(bytes.Buffer)
|
|
|
|
err := WriteServerMetadata(b)
|
|
assert.NoError(t, err)
|
|
assert.True(t, b.Len() > 0)
|
|
})
|
|
}
|