LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package state | 4 package state |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 "io/ioutil" | 8 "io/ioutil" |
9 "net/url" | 9 "net/url" |
10 "path/filepath" | 10 "path/filepath" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 st.transactionHooks <- converted | 49 st.transactionHooks <- converted |
50 c.Assert(original, HasLen, 0) | 50 c.Assert(original, HasLen, 0) |
51 return func() { | 51 return func() { |
52 remaining := <-st.transactionHooks | 52 remaining := <-st.transactionHooks |
53 st.transactionHooks <- nil | 53 st.transactionHooks <- nil |
54 c.Assert(remaining, HasLen, 0) | 54 c.Assert(remaining, HasLen, 0) |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 // SetBeforeHooks uses SetTransactionHooks to queue N functions to be run | 58 // SetBeforeHooks uses SetTransactionHooks to queue N functions to be run |
59 // immediately before the next N transactions. Nil values are accepted, and | 59 // immediately before the next N transactions. The first function is executed |
60 // useful, in that they can be used to ensure that a transaction is run at | 60 // before the first transaction, the second function before the second |
61 // the expected time, without having to make any changes or assert any state. | 61 // transaction and so on. Nil values are accepted, and useful, in that they can |
| 62 // be used to ensure that a transaction is run at the expected time, without |
| 63 // having to make any changes or assert any state. |
62 func SetBeforeHooks(c *C, st *State, fs ...func()) TransactionChecker { | 64 func SetBeforeHooks(c *C, st *State, fs ...func()) TransactionChecker { |
63 transactionHooks := make([]TransactionHook, len(fs)) | 65 transactionHooks := make([]TransactionHook, len(fs)) |
64 for i, f := range fs { | 66 for i, f := range fs { |
65 transactionHooks[i] = TransactionHook{Before: f} | 67 transactionHooks[i] = TransactionHook{Before: f} |
| 68 } |
| 69 return SetTransactionHooks(c, st, transactionHooks...) |
| 70 } |
| 71 |
| 72 // SetAfterHooks uses SetTransactionHooks to queue N functions to be run |
| 73 // immediately after the next N transactions. The first function is executed |
| 74 // after the first transaction, the second function after the second |
| 75 // transaction and so on. |
| 76 func SetAfterHooks(c *C, st *State, fs ...func()) TransactionChecker { |
| 77 transactionHooks := make([]TransactionHook, len(fs)) |
| 78 for i, f := range fs { |
| 79 transactionHooks[i] = TransactionHook{After: f} |
66 } | 80 } |
67 return SetTransactionHooks(c, st, transactionHooks...) | 81 return SetTransactionHooks(c, st, transactionHooks...) |
68 } | 82 } |
69 | 83 |
70 // SetRetryHooks uses SetTransactionHooks to inject a block function designed | 84 // SetRetryHooks uses SetTransactionHooks to inject a block function designed |
71 // to disrupt a transaction built against recent state, and a check function | 85 // to disrupt a transaction built against recent state, and a check function |
72 // designed to verify that the replacement transaction against the new state | 86 // designed to verify that the replacement transaction against the new state |
73 // has been applied as expected. | 87 // has been applied as expected. |
74 func SetRetryHooks(c *C, st *State, block, check func()) TransactionChecker { | 88 func SetRetryHooks(c *C, st *State, block, check func()) TransactionChecker { |
75 return SetTransactionHooks(c, st, TransactionHook{ | 89 return SetTransactionHooks(c, st, TransactionHook{ |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 172 |
159 // MinUnitsRevno returns the Revno of the minUnits document | 173 // MinUnitsRevno returns the Revno of the minUnits document |
160 // associated with the given service name. | 174 // associated with the given service name. |
161 func MinUnitsRevno(st *State, serviceName string) (int, error) { | 175 func MinUnitsRevno(st *State, serviceName string) (int, error) { |
162 var doc minUnitsDoc | 176 var doc minUnitsDoc |
163 if err := st.minUnits.FindId(serviceName).One(&doc); err != nil { | 177 if err := st.minUnits.FindId(serviceName).One(&doc); err != nil { |
164 return 0, err | 178 return 0, err |
165 } | 179 } |
166 return doc.Revno, nil | 180 return doc.Revno, nil |
167 } | 181 } |
LEFT | RIGHT |