Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data race in text ctx [checktoberfest][hacktoberfest] #102

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions pkg/allure/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"runtime"
"strings"
"sync"

"github.com/google/uuid"
"github.com/pkg/errors"
Expand All @@ -17,23 +18,27 @@ import (
// information about the test name, applications, description, status, references, labels,
// steps, containers, and time of the test execution.
type Result struct {
Name string `json:"name,omitempty"` // Test name
FullName string `json:"fullName,omitempty"` // Full path to the test
Stage string `json:"stage,omitempty"` // Stage of test execution
Status Status `json:"status,omitempty"` // Status of the test execution
StatusDetails StatusDetail `json:"statusDetails,omitempty"` // Details about the test (for example, errors during test execution will be recorded here)
Start int64 `json:"start,omitempty"` // Start of test execution
Stop int64 `json:"stop,omitempty"` // End of test execution
UUID uuid.UUID `json:"uuid,omitempty"` // Unique test ID
HistoryID string `json:"historyId,omitempty"` // ID in the allure history
TestCaseID string `json:"testCaseId,omitempty"` // ID of the test case (based on the hash of the full call)
Description string `json:"description,omitempty"` // Test description
Attachments []*Attachment `json:"attachments,omitempty"` // Test case attachments
Parameters []*Parameter `json:"parameters,omitempty"` // Test case parameters
Labels []*Label `json:"labels,omitempty"` // Array of labels
Links []*Link `json:"links,omitempty"` // Array of references
Steps []*Step `json:"steps,omitempty"` // Array of steps
ToPrint bool `json:"-"` // If false - the report will not be saved to a file
Name string `json:"name,omitempty"` // Test name
FullName string `json:"fullName,omitempty"` // Full path to the test
Stage string `json:"stage,omitempty"` // Stage of test execution
Status Status `json:"status,omitempty"` // Status of the test execution
StatusDetails StatusDetail `json:"statusDetails,omitempty"` // Details about the test (for example, errors during test execution will be recorded here)
Start int64 `json:"start,omitempty"` // Start of test execution
Stop int64 `json:"stop,omitempty"` // End of test execution
UUID uuid.UUID `json:"uuid,omitempty"` // Unique test ID
HistoryID string `json:"historyId,omitempty"` // ID in the allure history
TestCaseID string `json:"testCaseId,omitempty"` // ID of the test case (based on the hash of the full call)
Description string `json:"description,omitempty"` // Test description

m sync.RWMutex

Attachments []*Attachment `json:"attachments,omitempty"` // Test case attachments
Parameters []*Parameter `json:"parameters,omitempty"` // Test case parameters
Labels []*Label `json:"labels,omitempty"` // Array of labels
Links []*Link `json:"links,omitempty"` // Array of references
Steps []*Step `json:"steps,omitempty"` // Array of steps

ToPrint bool `json:"-"` // If false - the report will not be saved to a file
}

// NewResult Constructor Builds a new `allure.Result`. Sets the default values for the structure.
Expand All @@ -60,7 +65,7 @@ func NewResult(testName, fullName string) *Result {
ToPrint: true,
}
result.HistoryID = getMD5Hash(result.TestCaseID)
result.Labels = append(result.Labels, LanguageLabel(runtime.Version()))
result.AddLabel(LanguageLabel(runtime.Version()))
result.Begin()
return &result
}
Expand All @@ -82,12 +87,18 @@ func (result *Result) GetStatusTrace() string {
}

func (result *Result) addLabel(labelType LabelType, labelValue string) {
result.m.Lock()
defer result.m.Unlock()

label := NewLabel(labelType, labelValue)
result.Labels = append(result.Labels, label)
}

// AddLabel Adds all passed in arguments `allure.Label` to the report
func (result *Result) AddLabel(labels ...*Label) {
result.m.Lock()
defer result.m.Unlock()

result.Labels = append(result.Labels, labels...)
}

Expand All @@ -101,6 +112,9 @@ func (result *Result) GetFirstLabel(labelType LabelType) (label *Label, ok bool)

// GetLabels Returns all `allure.Label` whose `LabelType` matches the one specified in the argument.
func (result *Result) GetLabels(labelType LabelType) []*Label {
result.m.RLock()
defer result.m.RUnlock()

labels := make([]*Label, 0)
for _, label := range result.Labels {
if label.Name == labelType.ToString() {
Expand All @@ -116,6 +130,7 @@ func (result *Result) SetNewLabelMap(kv map[LabelType]string) {
for k, v := range kv {
labels = append(labels, NewLabel(k, v))
}

result.AddLabel(labels...)
}

Expand Down Expand Up @@ -197,6 +212,9 @@ func (result *Result) WithLabels(label ...*Label) *Result {
// WithLaunchTags Adds all Launch Tags from the global variable `ALLURE_LAUNCH_TAGS` as labels with type `Tag` to the report.
// Returns a pointer to the current `allure.Result` (for Fluent Interface).
func (result *Result) WithLaunchTags() *Result {
result.m.Lock()
defer result.m.Unlock()

if tags := os.Getenv(defaultTagsEnvKey); tags != "" {
for _, tag := range strings.Split(tags, ",") {
result.Labels = append(result.Labels, TagLabel(strings.Trim(tag, " ")))
Expand All @@ -223,10 +241,10 @@ func (result *Result) SkipOnPrint() {
}

// Print If `Result.ToPrint` = `true` - the method terminates without creating any files. Otherwise:
// - Calls `Result.PrintAttachments()`.
// - Saves the file `uuid4-Result.json`.
// - Calls `Result.Container.Print()`
// - Returns error (if any)
// - Calls `Result.PrintAttachments()`.
// - Saves the file `uuid4-Result.json`.
// - Calls `Result.Container.Print()`
// - Returns error (if any)
func (result *Result) Print() error {
if !result.ToPrint {
return nil
Expand Down Expand Up @@ -254,6 +272,9 @@ func (result *Result) printResult() error {
// for each allure.Step calls the `Step.PrintAttachments()` method.
// Then calls `Attachment.Print()` on all `allure.Attachment` of the `Result.Attachments` list.
func (result *Result) PrintAttachments() {
result.m.RLock()
defer result.m.RUnlock()

for _, step := range result.Steps {
step.PrintAttachments()
}
Expand All @@ -270,6 +291,7 @@ func (result *Result) Done() error {
if result.Status == "" {
result.Status = Passed
}

result.Finish()
return result.Print()
}
Expand All @@ -281,6 +303,9 @@ func (result *Result) ReplaceNewLabel(name LabelType, value string) {

// ReplaceLabel replaces label in the allure.Result object by label's name
func (result *Result) ReplaceLabel(label *Label) {
result.m.Lock()
defer result.m.Unlock()

for _, l := range result.Labels {
if label.Name == l.Name {
l.Value = label.Value
Expand Down
1 change: 0 additions & 1 deletion pkg/allure/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
)

func TestNewResult(t *testing.T) {

result := NewResult(testName, testFullName)
now := GetNow()

Expand Down
3 changes: 2 additions & 1 deletion pkg/framework/core/allure_manager/ctx/test_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type testCtx struct {
name string
name string

result *allure.Result
}

Expand Down
Loading