Move execution storage to new package
This commit is contained in:
19
pkg/execution/execution.go
Normal file
19
pkg/execution/execution.go
Normal file
@ -0,0 +1,19 @@
|
||||
package execution
|
||||
|
||||
import (
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
|
||||
)
|
||||
|
||||
// ID is an identifier for an execution.
|
||||
type ID string
|
||||
|
||||
// Storage stores executions.
|
||||
type Storage interface {
|
||||
// Add adds a runner to the storage.
|
||||
// It overwrites the existing execution if an execution with the same id already exists.
|
||||
Add(id ID, executionRequest *dto.ExecutionRequest)
|
||||
|
||||
// Pop deletes the execution with the given id from the storage and returns it.
|
||||
// If no such execution exists, ok is false and true otherwise.
|
||||
Pop(id ID) (request *dto.ExecutionRequest, ok bool)
|
||||
}
|
35
pkg/execution/local_storage.go
Normal file
35
pkg/execution/local_storage.go
Normal file
@ -0,0 +1,35 @@
|
||||
package execution
|
||||
|
||||
import (
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// localStorage stores execution objects in the local application memory.
|
||||
// ToDo: Create implementation that use some persistent storage like a database.
|
||||
type localStorage struct {
|
||||
sync.RWMutex
|
||||
executions map[ID]*dto.ExecutionRequest
|
||||
}
|
||||
|
||||
// NewLocalStorage responds with an Storage implementation.
|
||||
// This implementation stores the data thread-safe in the local application memory.
|
||||
func NewLocalStorage() *localStorage {
|
||||
return &localStorage{
|
||||
executions: make(map[ID]*dto.ExecutionRequest),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *localStorage) Add(id ID, executionRequest *dto.ExecutionRequest) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.executions[id] = executionRequest
|
||||
}
|
||||
|
||||
func (s *localStorage) Pop(id ID) (*dto.ExecutionRequest, bool) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
request, ok := s.executions[id]
|
||||
delete(s.executions, id)
|
||||
return request, ok
|
||||
}
|
70
pkg/execution/local_storage_test.go
Normal file
70
pkg/execution/local_storage_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
package execution
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
testExecutionRequest = dto.ExecutionRequest{
|
||||
Command: "echo 'Hello Poseidon'",
|
||||
TimeLimit: 42,
|
||||
Environment: map[string]string{
|
||||
"PATH": "/bin",
|
||||
},
|
||||
}
|
||||
anotherTestExecutionRequest = dto.ExecutionRequest{
|
||||
Command: "echo 'Bye Poseidon'",
|
||||
TimeLimit: 1337,
|
||||
Environment: nil,
|
||||
}
|
||||
testID = ID("test")
|
||||
)
|
||||
|
||||
func TestNewLocalExecutionStorage(t *testing.T) {
|
||||
storage := NewLocalStorage()
|
||||
assert.Zero(t, len(storage.executions))
|
||||
}
|
||||
|
||||
func TestLocalStorage(t *testing.T) {
|
||||
storage := NewLocalStorage()
|
||||
|
||||
t.Run("cannot pop when id does not exist", func(t *testing.T) {
|
||||
request, ok := storage.Pop(testID)
|
||||
assert.False(t, ok)
|
||||
assert.Nil(t, request)
|
||||
})
|
||||
|
||||
t.Run("adds execution request", func(t *testing.T) {
|
||||
storage.Add(testID, &testExecutionRequest)
|
||||
|
||||
request, ok := storage.executions[testID]
|
||||
assert.Equal(t, len(storage.executions), 1)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, testExecutionRequest, *request)
|
||||
})
|
||||
|
||||
t.Run("overwrites execution request", func(t *testing.T) {
|
||||
storage.Add(testID, &anotherTestExecutionRequest)
|
||||
|
||||
request, ok := storage.executions[testID]
|
||||
assert.Equal(t, len(storage.executions), 1)
|
||||
assert.True(t, ok)
|
||||
require.NotNil(t, request)
|
||||
assert.Equal(t, anotherTestExecutionRequest, *request)
|
||||
})
|
||||
|
||||
t.Run("removes execution request", func(t *testing.T) {
|
||||
request, ok := storage.Pop(testID)
|
||||
|
||||
assert.True(t, ok)
|
||||
require.NotNil(t, request)
|
||||
assert.Equal(t, anotherTestExecutionRequest, *request)
|
||||
|
||||
request, ok = storage.executions[testID]
|
||||
assert.Nil(t, request)
|
||||
assert.False(t, ok)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user