MCPcopy
hub / github.com/sirupsen/logrus / TestEntryWithContextCopiesData

Function TestEntryWithContextCopiesData

entry_test.go:53–94  ·  entry_test.go::TestEntryWithContextCopiesData
(t *testing.T)

Source from the content-addressed store, hash-verified

51}
52
53func TestEntryWithContextCopiesData(t *testing.T) {
54 assert := assert.New(t)
55
56 // Initialize a parent Entry object with a key/value set in its Data map
57 logger := New()
58 logger.Out = &bytes.Buffer{}
59 parentEntry := NewEntry(logger).WithField("parentKey", "parentValue")
60
61 // Create two children Entry objects from the parent in different contexts
62 var contextKey1 contextKeyType = "foo"
63 ctx1 := context.WithValue(context.Background(), contextKey1, "bar")
64 childEntry1 := parentEntry.WithContext(ctx1)
65 assert.Equal(ctx1, childEntry1.Context)
66
67 var contextKey2 contextKeyType = "bar"
68 ctx2 := context.WithValue(context.Background(), contextKey2, "baz")
69 childEntry2 := parentEntry.WithContext(ctx2)
70 assert.Equal(ctx2, childEntry2.Context)
71 assert.NotEqual(ctx1, ctx2)
72
73 // Ensure that data set in the parent Entry are preserved to both children
74 assert.Equal("parentValue", childEntry1.Data["parentKey"])
75 assert.Equal("parentValue", childEntry2.Data["parentKey"])
76
77 // Modify data stored in the child entry
78 childEntry1.Data["childKey"] = "childValue"
79
80 // Verify that data is successfully stored in the child it was set on
81 val, exists := childEntry1.Data["childKey"]
82 assert.True(exists)
83 assert.Equal("childValue", val)
84
85 // Verify that the data change to child 1 has not affected its sibling
86 val, exists = childEntry2.Data["childKey"]
87 assert.False(exists)
88 assert.Empty(val)
89
90 // Verify that the data change to child 1 has not affected its parent
91 val, exists = parentEntry.Data["childKey"]
92 assert.False(exists)
93 assert.Empty(val)
94}
95
96func TestEntryWithTimeCopiesData(t *testing.T) {
97 assert := assert.New(t)

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
NewEntryFunction · 0.85
WithFieldMethod · 0.65
WithContextMethod · 0.45

Tested by

no test coverage detected