mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-25 00:29:27 +00:00
Co-authored-by: Lukas <lukas@slucky.de>
This commit is contained in:
parent
6f5e0697d0
commit
4576aef483
2 changed files with 52 additions and 2 deletions
|
@ -510,8 +510,8 @@ func (s *RPC) getHostnameFromContext(ctx context.Context) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RPC) updateAgentLastWork(agent *model.Agent) error {
|
func (s *RPC) updateAgentLastWork(agent *model.Agent) error {
|
||||||
// only update agent.LastWork if not done recently
|
// only update agent.LastWork if not recently updated
|
||||||
if time.Unix(agent.LastWork, 0).Add(1 * time.Minute).Before(time.Now()) {
|
if time.Unix(agent.LastWork, 0).Add(time.Minute).After(time.Now()) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,11 @@ package grpc
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/franela/goblin"
|
"github.com/franela/goblin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
@ -107,3 +109,51 @@ func TestRegisterAgent(t *testing.T) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateAgentLastWork(t *testing.T) {
|
||||||
|
t.Run("When last work was never updated it should update last work timestamp", func(t *testing.T) {
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: 0,
|
||||||
|
}
|
||||||
|
store := mocks_store.NewStore(t)
|
||||||
|
rpc := RPC{
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
store.On("AgentUpdate", mock.Anything).Once().Return(nil)
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NotZero(t, agent.LastWork)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When last work was updated over a minute ago it should update last work timestamp", func(t *testing.T) {
|
||||||
|
lastWork := time.Now().Add(-time.Hour).Unix()
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: lastWork,
|
||||||
|
}
|
||||||
|
store := mocks_store.NewStore(t)
|
||||||
|
rpc := RPC{
|
||||||
|
store: store,
|
||||||
|
}
|
||||||
|
store.On("AgentUpdate", mock.Anything).Once().Return(nil)
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NotEqual(t, lastWork, agent.LastWork)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When last work was updated in the last minute it should not update last work timestamp again", func(t *testing.T) {
|
||||||
|
lastWork := time.Now().Add(-time.Second * 30).Unix()
|
||||||
|
agent := model.Agent{
|
||||||
|
LastWork: lastWork,
|
||||||
|
}
|
||||||
|
rpc := RPC{}
|
||||||
|
|
||||||
|
err := rpc.updateAgentLastWork(&agent)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, lastWork, agent.LastWork)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue