@@ -132,13 +132,11 @@ def quota(self, amount):
132132 self .quota_consumed = 0
133133 self .max_quota = 0
134134 elif delta < 0 :
135- # Quota being filled for the new period
136- assert self .quota_consumed == 0
137- self .max_quota = amount
135+ # Quota being filled during the period
136+ self .max_quota = amount + self .quota_consumed
138137 else :
139138 # Quota being consumed during the period
140- assert self .max_quota >= self .quota_consumed
141- self .quota_consumed += delta
139+ self .quota_consumed = delta
142140
143141
144142# Example 10
@@ -160,3 +158,47 @@ def quota(self, amount):
160158 print ('Not enough for 3 quota' )
161159
162160print ('Still' , bucket )
161+
162+
163+ # Example 11
164+ bucket = NewBucket (6000 )
165+ assert bucket .max_quota == 0
166+ assert bucket .quota_consumed == 0
167+ assert bucket .quota == 0
168+
169+ fill (bucket , 100 )
170+ assert bucket .max_quota == 100
171+ assert bucket .quota_consumed == 0
172+ assert bucket .quota == 100
173+
174+ assert deduct (bucket , 10 )
175+ assert bucket .max_quota == 100
176+ assert bucket .quota_consumed == 10
177+ assert bucket .quota == 90
178+
179+ assert deduct (bucket , 20 )
180+ assert bucket .max_quota == 100
181+ assert bucket .quota_consumed == 30
182+ assert bucket .quota == 70
183+
184+ fill (bucket , 50 )
185+ assert bucket .max_quota == 150
186+ assert bucket .quota_consumed == 30
187+ assert bucket .quota == 120
188+
189+ assert deduct (bucket , 40 )
190+ assert bucket .max_quota == 150
191+ assert bucket .quota_consumed == 70
192+ assert bucket .quota == 80
193+
194+ assert not deduct (bucket , 81 )
195+ assert bucket .max_quota == 150
196+ assert bucket .quota_consumed == 70
197+ assert bucket .quota == 80
198+
199+ bucket .reset_time += bucket .period_delta - timedelta (1 )
200+ assert bucket .quota == 80
201+ assert not deduct (bucket , 79 )
202+
203+ fill (bucket , 1 )
204+ assert bucket .quota == 1
0 commit comments