Skip to content

Commit

Permalink
chore(plc4go): re-order time declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jun 16, 2023
1 parent adeac04 commit c02f8f5
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 92 deletions.
2 changes: 1 addition & 1 deletion plc4go/examples/ads/subscribe/Subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func main() {
print(responseCode)
}

time.Sleep(time.Second * 200)
time.Sleep(200 * time.Second)

}
6 changes: 3 additions & 3 deletions plc4go/internal/cbus/Browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (m *Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]an
if err != nil {
return nil, errors.Wrap(err, "Error subscribing to the installation MMI")
}
subCtx, subCtxCancel := context.WithTimeout(ctx, time.Second*2)
subCtx, subCtxCancel := context.WithTimeout(ctx, 2*time.Second)
defer subCtxCancel()
subscriptionResult := <-subscriptionRequest.ExecuteWithContext(subCtx)
if err := subscriptionResult.GetErr(); err != nil {
Expand Down Expand Up @@ -296,7 +296,7 @@ func (m *Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]an
if err != nil {
return nil, errors.Wrap(err, "Error building the installation MMI")
}
readCtx, readCtxCancel := context.WithTimeout(ctx, time.Second*2)
readCtx, readCtxCancel := context.WithTimeout(ctx, 2*time.Second)
defer readCtxCancel()
readWg := sync.WaitGroup{}
readWg.Add(1)
Expand Down Expand Up @@ -373,7 +373,7 @@ func (m *Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]an
}
}()

syncCtx, syncCtxCancel := context.WithTimeout(ctx, time.Second*6)
syncCtx, syncCtxCancel := context.WithTimeout(ctx, 6*time.Second)
defer syncCtxCancel()
for !blockOffset0Received || !blockOffset88Received || !blockOffset176Received {
select {
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/cbus/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func (c *Connection) sendCalDataWrite(ctx context.Context, ch chan plc4go.PlcCon
}

startTime := time.Now()
timeout := time.NewTimer(time.Second * 2)
timeout := time.NewTimer(2 * time.Second)
defer utils.CleanupTimer(timeout)
select {
case <-directCommandAckChan:
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/cbus/Connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func TestConnection_fireConnected(t *testing.T) {
},
args: args{ch: make(chan<- plc4go.PlcConnectionConnectResult, 1)},
chanValidator: func(t *testing.T, results chan<- plc4go.PlcConnectionConnectResult) bool {
time.Sleep(time.Millisecond * 50)
time.Sleep(50 * time.Millisecond)
return len(results) == 1
},
},
Expand Down Expand Up @@ -827,7 +827,7 @@ func TestConnection_fireConnectionError(t *testing.T) {
},
args: args{ch: make(chan<- plc4go.PlcConnectionConnectResult, 1)},
chanValidator: func(t *testing.T, results chan<- plc4go.PlcConnectionConnectResult) bool {
time.Sleep(time.Millisecond * 50)
time.Sleep(50 * time.Millisecond)
return len(results) == 1
},
},
Expand Down
6 changes: 3 additions & 3 deletions plc4go/internal/cbus/Discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ func (d *Discoverer) createDeviceScanDispatcher(tcpTransportInstance *tcp.Transp
}
// Keep on reading responses till the timeout is done.
// TODO: Make this configurable
timeout := time.NewTimer(time.Second * 1)
timeout := time.NewTimer(1 * time.Second)
defer utils.CleanupTimer(timeout)
for start := time.Now(); time.Since(start) < time.Second*5; {
timeout.Reset(time.Second * 1)
for start := time.Now(); time.Since(start) < 5*time.Second; {
timeout.Reset(1 * time.Second)
select {
case receivedMessage := <-codec.GetDefaultIncomingMessageChannel():
// Cleanup, going to be resetted again
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/cbus/Reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (m *Reader) createMessageTransactionAndWait(ctx context.Context, messageToS
func (m *Reader) sendMessageOverTheWire(ctx context.Context, transaction transactions.RequestTransaction, messageToSend readWriteModel.CBusMessage, addResponseCode func(name string, responseCode apiModel.PlcResponseCode), tagName string, addPlcValue func(name string, plcValue apiValues.PlcValue)) {
// Send the over the wire
m.log.Trace().Msg("send over the wire")
ttl := time.Second * 5
ttl := 5 * time.Second
if deadline, ok := ctx.Deadline(); ok {
ttl = -time.Since(deadline)
m.log.Debug().Msgf("setting ttl to %s", ttl)
Expand Down Expand Up @@ -233,7 +233,7 @@ func (m *Reader) sendMessageOverTheWire(ctx context.Context, transaction transac
ttl); err != nil {
m.log.Debug().Err(err).Msgf("Error sending message for tag %s", tagName)
addResponseCode(tagName, apiModel.PlcResponseCode_INTERNAL_ERROR)
if err := transaction.FailRequest(errors.Errorf("timeout after %s", time.Second*1)); err != nil {
if err := transaction.FailRequest(errors.Errorf("timeout after %s", 1*time.Second)); err != nil {
m.log.Debug().Err(err).Msg("Error failing request")
}
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/knxnetip/Browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (m Browser) executeDeviceQuery(ctx context.Context, query DeviceQuery, inte
queryResults = append(queryResults, queryResult)
}

disconnectTtlTimer := time.NewTimer(m.connection.defaultTtl * 10)
disconnectTtlTimer := time.NewTimer(10 * m.connection.defaultTtl)
deviceDisconnections := m.connection.DeviceDisconnect(ctx, knxAddress)
select {
case _ = <-deviceDisconnections:
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/knxnetip/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func NewConnection(transportInstance transports.TransportInstance, connectionOpt
valueCache: map[uint16][]byte{},
valueCacheMutex: sync.RWMutex{},
metadata: &ConnectionMetadata{},
defaultTtl: time.Second * 10,
defaultTtl: 10 * time.Second,
DeviceConnections: map[driverModel.KnxAddress]*KnxDeviceConnection{},
handleTunnelingRequests: true,
passLogToModel: options.ExtractPassLoggerToModel(_options...),
Expand Down
14 changes: 7 additions & 7 deletions plc4go/internal/knxnetip/ConnectionDriverSpecificOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *Connection) ReadGroupAddress(ctx context.Context, groupAddress []byte,
result := make(chan KnxReadResult, 1)

sendResponse := func(value values.PlcValue, numItems uint8, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxReadResult{
value: value,
Expand Down Expand Up @@ -110,7 +110,7 @@ func (m *Connection) DeviceConnect(ctx context.Context, targetAddress driverMode
result := make(chan KnxDeviceConnectResult, 1)

sendResponse := func(connection *KnxDeviceConnection, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxDeviceConnectResult{
connection: connection,
Expand Down Expand Up @@ -206,7 +206,7 @@ func (m *Connection) DeviceDisconnect(ctx context.Context, targetAddress driverM
result := make(chan KnxDeviceDisconnectResult, 1)

sendResponse := func(connection *KnxDeviceConnection, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxDeviceDisconnectResult{
connection: connection,
Expand Down Expand Up @@ -245,7 +245,7 @@ func (m *Connection) DeviceAuthenticate(ctx context.Context, targetAddress drive
result := make(chan KnxDeviceAuthenticateResult, 1)

sendResponse := func(err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxDeviceAuthenticateResult{
err: err,
Expand Down Expand Up @@ -303,7 +303,7 @@ func (m *Connection) DeviceReadProperty(ctx context.Context, targetAddress drive
result := make(chan KnxReadResult, 1)

sendResponse := func(value values.PlcValue, numItems uint8, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxReadResult{
value: value,
Expand Down Expand Up @@ -388,7 +388,7 @@ func (m *Connection) DeviceReadPropertyDescriptor(ctx context.Context, targetAdd
result := make(chan KnxReadResult, 1)

sendResponse := func(value values.PlcValue, numItems uint8, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxReadResult{
value: value,
Expand Down Expand Up @@ -453,7 +453,7 @@ func (m *Connection) DeviceReadMemory(ctx context.Context, targetAddress driverM
result := make(chan KnxReadResult, 1)

sendResponse := func(value values.PlcValue, numItems uint8, err error) {
timeout := time.NewTimer(time.Millisecond * 10)
timeout := time.NewTimer(10 * time.Millisecond)
select {
case result <- KnxReadResult{
value: value,
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/knxnetip/Discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ func (d *Discoverer) createDeviceScanDispatcher(udpTransportInstance *udp.Transp
}
// Keep on reading responses till the timeout is done.
// TODO: Make this configurable
timeout := time.NewTimer(time.Second * 1)
timeout := time.NewTimer(1 * time.Second)
timeout.Stop()
for start := time.Now(); time.Since(start) < time.Second*5; {
timeout.Reset(time.Second * 1)
timeout.Reset(1 * time.Second)
select {
case message := <-codec.GetDefaultIncomingMessageChannel():
{
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/modbus/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, connecti
}
}
connection.DefaultConnection = _default.NewDefaultConnection(connection,
_default.WithDefaultTtl(time.Second*5),
_default.WithDefaultTtl(5*time.Second),
_default.WithPlcTagHandler(tagHandler),
_default.WithPlcValueHandler(NewValueHandler(_options...)),
)
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/s7/Reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (m *Reader) Read(ctx context.Context, readRequest apiModel.PlcReadRequest)
nil,
errors.Wrap(err, "error sending message"),
)
if err := transaction.FailRequest(errors.Errorf("timeout after %s", time.Second*1)); err != nil {
if err := transaction.FailRequest(errors.Errorf("timeout after %s", 1*time.Second)); err != nil {
m.log.Debug().Err(err).Msg("Error failing request")
}
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/s7/Writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (m Writer) Write(ctx context.Context, writeRequest apiModel.PlcWriteRequest
return transaction.EndRequest()
}, time.Second*1); err != nil {
result <- spiModel.NewDefaultPlcWriteRequestResult(writeRequest, nil, errors.Wrap(err, "error sending message"))
if err := transaction.FailRequest(errors.Errorf("timeout after %s", time.Second*1)); err != nil {
if err := transaction.FailRequest(errors.Errorf("timeout after %s", 1*time.Second)); err != nil {
m.log.Debug().Err(err).Msg("Error failing request")
}
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/simulated/Connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestConnection_Connect(t *testing.T) {
},
connected: true,
}, nil),
delayAtLeast: time.Second * 1,
delayAtLeast: 1 * time.Second,
wantErr: false,
},
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/pkg/api/cache/PlcConnectionCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewPlcConnectionCache(driverManager plc4go.PlcDriverManager, withConnection
if !config.TraceConnectionCache {
log = zerolog.Nop()
}
maxLeaseTime := time.Second * 5
maxLeaseTime := 5 * time.Second
cc := &plcConnectionCache{
log: log,
driverManager: driverManager,
Expand Down
48 changes: 24 additions & 24 deletions plc4go/pkg/api/cache/PlcConnectionCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ func TestPlcConnectionCache_ReusingAnExistingConnection(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand Down Expand Up @@ -353,8 +353,8 @@ func TestPlcConnectionCache_MultipleConcurrentConnectionRequests(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand All @@ -377,7 +377,7 @@ func TestPlcConnectionCache_MultipleConcurrentConnectionRequests(t *testing.T) {
"ping-success",
}, 1)

time.Sleep(time.Millisecond * 1)
time.Sleep(1 * time.Millisecond)

// Almost instantly request the same connection for a second time.
// As the connection takes 100ms, the second connection request will come
Expand Down Expand Up @@ -427,8 +427,8 @@ func TestPlcConnectionCache_ConnectWithError(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand Down Expand Up @@ -467,8 +467,8 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand Down Expand Up @@ -528,8 +528,8 @@ func TestPlcConnectionCache_PingTimeout(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand Down Expand Up @@ -573,8 +573,8 @@ func TestPlcConnectionCache_SecondCallGetNewConnectionAfterPingTimeout(t *testin
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand Down Expand Up @@ -651,8 +651,8 @@ func TestPlcConnectionCache_FistReadGivesUpBeforeItGetsTheConnectionSoSecondOneT
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand All @@ -667,7 +667,7 @@ func TestPlcConnectionCache_FistReadGivesUpBeforeItGetsTheConnectionSoSecondOneT
// Intentionally just ignore the response.
cache.GetConnection("simulated://1.2.3.4:42?connectionDelay=100&traceEnabled=true")

time.Sleep(time.Millisecond * 1)
time.Sleep(1 * time.Millisecond)

// Read once from the cache.
// NOTE: It doesn't contain the connect-part, as the previous connection handled that.
Expand Down Expand Up @@ -696,8 +696,8 @@ func TestPlcConnectionCache_SecondConnectionGivenUpWaiting(t *testing.T) {
driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 5,
maxWaitTime: time.Second * 25,
maxLeaseTime: 5 * time.Second,
maxWaitTime: 25 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand All @@ -720,7 +720,7 @@ func TestPlcConnectionCache_SecondConnectionGivenUpWaiting(t *testing.T) {
"ping-success",
}, 1)

time.Sleep(time.Millisecond * 1)
time.Sleep(1 * time.Millisecond)

// Almost instantly we try to get a new connection but don't listen for the result
cache.GetConnection("simulated://1.2.3.4:42?connectionDelay=100&traceEnabled=true")
Expand All @@ -733,7 +733,7 @@ func TestPlcConnectionCache_SecondConnectionGivenUpWaiting(t *testing.T) {
}

// Wait for 1s to have the connection cache timeout (10ms) the lease as nobody's listening.
time.Sleep(time.Millisecond * 1000)
time.Sleep(1 * time.Second)

// This should be quite equal to the serial case as the connections are requested serially.
assert.NotNil(t, cache.GetTracer(), "Tracer should be available")
Expand Down Expand Up @@ -770,8 +770,8 @@ func TestPlcConnectionCache_MaximumWaitTimeReached(t *testing.T) {
// Reduce the max lease time as this way we also reduce the max wait time.
cache := plcConnectionCache{
driverManager: driverManager,
maxLeaseTime: time.Second * 1,
maxWaitTime: time.Second * 5,
maxLeaseTime: 1 * time.Second,
maxWaitTime: 5 * time.Second,
cacheLock: lock.NewCASMutex(),
connections: make(map[string]*connectionContainer),
tracer: nil,
Expand All @@ -786,11 +786,11 @@ func TestPlcConnectionCache_MaximumWaitTimeReached(t *testing.T) {
// The first and second connection should work fine
firstConnectionResults := cache.GetConnection("simulated://1.2.3.4:42?connectionDelay=100&pingDelay=4000&traceEnabled=true")

time.Sleep(time.Millisecond * 1)
time.Sleep(1 * time.Millisecond)

secondConnectionResults := cache.GetConnection("simulated://1.2.3.4:42?connectionDelay=100&pingDelay=4000&traceEnabled=true")

time.Sleep(time.Millisecond * 1)
time.Sleep(1 * time.Millisecond)

// The third connection should be given up by the cache
thirdConnectionResults := cache.GetConnection("simulated://1.2.3.4:42?connectionDelay=100&pingDelay=4000&traceEnabled=true")
Expand Down
2 changes: 1 addition & 1 deletion plc4go/pkg/api/cache/plcConnectionLease.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (t *plcConnectionLease) Close() <-chan plc4go.PlcConnectionCloseResult {
go func() {
// Check if the connection is still alive, if it is, put it back into the cache
pingResults := t.Ping()
pingTimeout := time.NewTimer(time.Second * 5)
pingTimeout := time.NewTimer(5 * time.Second)
newState := StateIdle
select {
case pingResult := <-pingResults:
Expand Down

0 comments on commit c02f8f5

Please sign in to comment.