MCPcopy Create free account
hub / github.com/DoNewsCode/core / TestFactory

Function TestFactory

di/factory_test.go:14–47  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestFactory(t *testing.T) {
15 t.Parallel()
16 var closed []string
17
18 f := NewFactory(func(name string) (Pair, error) {
19 nameCopy := name
20 return Pair{
21 Conn: &nameCopy,
22 Closer: func() {
23 closed = append(closed, name)
24 },
25 }, nil
26 })
27
28 foo, err := f.Make("foo")
29 assert.NoError(t, err)
30 assert.Equal(t, *(foo.(*string)), "foo")
31
32 bar, err := f.Make("bar")
33 assert.NoError(t, err)
34 assert.Equal(t, *(bar.(*string)), "bar")
35
36 bar2, err := f.Make("bar")
37 assert.NoError(t, err)
38 assert.Equal(t, bar, bar2)
39
40 assert.Len(t, f.List(), 2)
41
42 f.CloseConn("foo")
43 assert.Contains(t, closed, "foo")
44
45 f.Close()
46 assert.Contains(t, closed, "foo", "bar")
47}
48
49func TestFactory_nilCloser(t *testing.T) {
50 t.Parallel()

Callers

nothing calls this directly

Calls 6

MakeMethod · 0.95
ListMethod · 0.95
CloseConnMethod · 0.95
CloseMethod · 0.95
NewFactoryFunction · 0.85
LenMethod · 0.45

Tested by

no test coverage detected