1 parent
3f6536bc87
commit
6de9fb1ebc
4 files changed
+77
-1
No files matched your search
@@ -116,7 +116,10 @@ func Current() *Service {
|
||||
return current.value
|
||||
}
|
||||
|
||||
func (s *Service) Preview(_ context.Context, source config.DatabaseConfig, currentGeneration uint64, targetName string) (Record, string, error) {
|
||||
func (s *Service) Preview(ctx context.Context, source config.DatabaseConfig, currentGeneration uint64, targetName string) (Record, string, error) {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return Record{}, "", err
|
||||
}
|
||||
if s == nil || currentGeneration == 0 {
|
||||
return Record{}, "", ErrInvalidRequest
|
||||
}
|
||||
@@ -139,6 +142,9 @@ func (s *Service) Preview(_ context.Context, source config.DatabaseConfig, curre
|
||||
if err != nil {
|
||||
return Record{}, "", ErrInvalidRequest
|
||||
}
|
||||
if err := contextError(ctx); err != nil {
|
||||
return Record{}, "", err
|
||||
}
|
||||
now := s.currentTime()
|
||||
item := &record{Record: Record{ID: id, Status: StatusPreviewed, Source: plan.Source, Target: plan.Target, CurrentGeneration: currentGeneration, CreatedAt: now, UpdatedAt: now}, tokenHash: sha256.Sum256([]byte(token)), expiresAt: now.Add(previewLifetime), sourceConfig: plan.sourceConfig, targetConfig: plan.targetConfig}
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -81,6 +81,21 @@ func TestPreviewRejectsUnsafeTargetName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewHonorsCanceledContextWithoutCreatingRecord(t *testing.T) {
|
||||
service := NewService(testRunner{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
if _, _, err := service.Preview(ctx, testDatabase(t), 1, "smail_next"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("canceled preview returned %v, want context canceled", err)
|
||||
}
|
||||
service.mu.RLock()
|
||||
defer service.mu.RUnlock()
|
||||
if len(service.records) != 0 {
|
||||
t.Fatalf("canceled preview created %d records", len(service.records))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedExecutionKeepsSafeState(t *testing.T) {
|
||||
service := NewService(nil)
|
||||
record, token, err := service.Preview(context.Background(), testDatabase(t), 1, "smail_next")
|
||||
|
||||
Reference in new issue
Block a user