mirror of https://github.com/hashicorp/boundary
worker: ensure connections are closed in local state on controller fail (#1369)
This backports some test work that is being added in #1340 to assert that the worker is actually marking connections closed in its *local* state on a failure to contact the controller. This was missed in #1357 during some late-stage refactoring. Summary of the changes: * If we can't actually contact the controller during connection closure to mark the connections as closed, we actually construct a "fake" response that just simply includes a closed state for all connections requested. This is then used to update the local connection state. * Added a 3 second timeout to the CloseConnection request to the controller to ensure the call to close connections completed in a timely manner and did not hang indefinitely. Testing enhancements: * Added a new helper in TestHelper called LookupSession that allows the lookup of a session from the worker's internal session state. * This is used in the new ExpectConnectionStateOnWorker check in the E2E test, which is part of a larger amount of enhancements to the E2E test that are coming in #1340. * Added a ConnectionStatusFromProtoVal helper function to the session package to allow the conversion of protobuf connection status values to their higher-level counterparts; essentially it is a reverse of ConnectionStatus.ProtoVal. This is used in some of our tests (with more coming in #1340) to just help with keeping things more idiomatic. Finally, there is some minor error refactoring, and we also change the timeout on internal status and connection close requests to the controller to 5 seconds.build-48e55f156a0fbdcb4e1e711b04271e57bc8f952e-7706fefd870195c1
parent
b3a74584ca
commit
48e55f156a
@ -0,0 +1,61 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTestWorkerLookupSession(t *testing.T) {
|
||||
require := require.New(t)
|
||||
// This loads the golang reference time, see those docs for more details. We
|
||||
// just use this as a stable non-zero time.
|
||||
refTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
|
||||
require.NoError(err)
|
||||
|
||||
tw := new(TestWorker)
|
||||
tw.w = &Worker{
|
||||
sessionInfoMap: new(sync.Map),
|
||||
}
|
||||
tw.w.sessionInfoMap.Store("foo", &sessionInfo{
|
||||
id: "foo",
|
||||
status: pbs.SESSIONSTATUS_SESSIONSTATUS_ACTIVE,
|
||||
connInfoMap: map[string]*connInfo{
|
||||
"one": &connInfo{
|
||||
id: "one",
|
||||
status: pbs.CONNECTIONSTATUS_CONNECTIONSTATUS_CLOSED,
|
||||
closeTime: refTime,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := TestSessionInfo{
|
||||
Id: "foo",
|
||||
Status: pbs.SESSIONSTATUS_SESSIONSTATUS_ACTIVE,
|
||||
Connections: map[string]TestConnectionInfo{
|
||||
"one": TestConnectionInfo{
|
||||
Id: "one",
|
||||
Status: pbs.CONNECTIONSTATUS_CONNECTIONSTATUS_CLOSED,
|
||||
CloseTime: refTime,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, ok := tw.LookupSession("foo")
|
||||
require.True(ok)
|
||||
require.Equal(expected, actual)
|
||||
}
|
||||
|
||||
func TestTestWorkerLookupSessionMissing(t *testing.T) {
|
||||
require := require.New(t)
|
||||
tw := new(TestWorker)
|
||||
tw.w = &Worker{
|
||||
sessionInfoMap: new(sync.Map),
|
||||
}
|
||||
actual, ok := tw.LookupSession("missing")
|
||||
require.False(ok)
|
||||
require.Equal(TestSessionInfo{}, actual)
|
||||
}
|
||||
Loading…
Reference in new issue