MCPcopy
hub / github.com/dropbox/godropbox / TestGet

Method TestGet

singleton/singleton_test.go:20–53  ·  view source on GitHub ↗
(c *C)

Source from the content-addressed store, hash-verified

18var _ = Suite(&singletonSuite{})
19
20func (*singletonSuite) TestGet(c *C) {
21 callCount := 0
22 init := func() (interface{}, error) {
23 callCount++
24
25 if callCount < 2 {
26 return nil, errors.Newf("fail")
27 }
28 v := 5
29 return &v, nil
30 }
31
32 s := NewSingleton(init)
33 // Verify init hasn't been called yet
34 c.Assert(callCount, Equals, 0)
35
36 // Verify init gets called with Get()
37 _, err := s.Get()
38 c.Assert(err, NotNil)
39 c.Assert(callCount, Equals, 1)
40
41 // Verify failed init gets retried with Get()
42 data1, err := s.Get()
43 c.Assert(err, IsNil)
44 c.Assert(callCount, Equals, 2)
45 c.Assert(*(data1.(*int)), Equals, 5)
46
47 // Verify that subsequent Get() after a successful one return the same object and don't call
48 // init
49 data2, err := s.Get()
50 c.Assert(err, IsNil)
51 c.Assert(callCount, Equals, 2)
52 c.Assert(data1, Equals, data2)
53}

Callers

nothing calls this directly

Calls 3

NewfFunction · 0.92
NewSingletonFunction · 0.85
GetMethod · 0.65

Tested by

no test coverage detected