Skip to content

Commit

Permalink
tests(definition): include GetDefinitions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldtinoco committed Jul 31, 2023
1 parent 80c9fce commit ec65097
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions pkg/events/definition_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,24 @@ func TestDefinitionGroup_Length(t *testing.T) {
require.Equal(t, defGroup.Length(), 2) // definition group with two definitions
}

// TODO: func TestDefinitionGroup_GetDefinitions(t *testing.T)
// TestDefinitionGroup_GetDefinitions tests that GetDefinitions returns a map of definition IDs to their definitions.
func TestDefinitionGroup_GetDefinitions(t *testing.T) {
defGroup := NewDefinitionGroup()

id1 := ID(1)
id2 := ID(2)

def1 := NewDefinition(id1, id1+1000, "def1", "", false, false, []string{}, Dependencies{}, nil)
def2 := NewDefinition(id2, id2+1000, "def2", "", false, false, []string{}, Dependencies{}, nil)

defGroup.AddBatch(map[ID]Definition{id1: def1, id2: def2})

defs := defGroup.GetDefinitions()

require.Equal(t, len(defs), len(defGroup.definitions)) // same number of definitions
require.Contains(t, defs, def1) // same definition
require.Contains(t, defs, def2) // same definition
}

// TestDefinitionGroup_NamesToIDs tests that NamesToIDs returns a map of definition names to their IDs.
func TestDefinitionGroup_NamesToIDs(t *testing.T) {
Expand Down Expand Up @@ -217,7 +234,29 @@ func TestDefinitionGroup_Length_MultipleThreads(t *testing.T) {
require.Equal(t, amountOfTestThreads, defGroup.Length()) // definition group with 20 definitions
}

// TODO: func TestDefinitionGroup_GetDefinitions_MultipleThreads(t *testing.T)
// TestDefinitionGroup_GetDefinitions_MultipleThread tests that GetDefinitions is thread-safe.
func TestDefinitionGroup_GetDefinitions_MultipleThread(t *testing.T) {
defGroup := NewDefinitionGroup()

names := getNames()

wg := &sync.WaitGroup{}

for name, id := range names {
wg.Add(1)
go func(name string, id ID) {
def := NewDefinition(id, id+1000, name, "", false, false, []string{}, Dependencies{}, nil)
err := defGroup.AddBatch(map[ID]Definition{id: def})
require.NoError(t, err)
defGroup.GetDefinitions() // concurrent calls
wg.Done()
}(name, id)
}

wg.Wait()

require.Equal(t, amountOfTestThreads, defGroup.Length()) // definition group with 20 definitions
}

// TestDefinitionGroup_NamesToIDs_MultipleThreads tests that NamesToIDs is thread-safe.
func TestDefinitionGroup_NamesToIDs_MultipleThreads(t *testing.T) {
Expand Down

0 comments on commit ec65097

Please sign in to comment.