| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package service |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"strings" |
| 23 |
|
| 24 |
. "gopkg.in/check.v1" |
| 25 |
) |
| 26 |
|
| 27 |
type mBoxSuite struct { |
| 28 |
prevMBoxMaxMessagesSize int |
| 29 |
} |
| 30 |
|
| 31 |
var _ = Suite(&mBoxSuite{}) |
| 32 |
|
| 33 |
func (s *mBoxSuite) SetUpSuite(c *C) { |
| 34 |
s.prevMBoxMaxMessagesSize = mBoxMaxMessagesSize |
| 35 |
mBoxMaxMessagesSize = 100 |
| 36 |
} |
| 37 |
|
| 38 |
func (s *mBoxSuite) TearDownSuite(c *C) { |
| 39 |
mBoxMaxMessagesSize = s.prevMBoxMaxMessagesSize |
| 40 |
} |
| 41 |
|
| 42 |
func (s *mBoxSuite) TestAppend(c *C) { |
| 43 |
mbox := &mBox{} |
| 44 |
m1 := json.RawMessage(`{"m":1}`) |
| 45 |
m2 := json.RawMessage(`{"m":2}`) |
| 46 |
mbox.Append(m1, "n1") |
| 47 |
mbox.Append(m2, "n2") |
| 48 |
c.Check(mbox.messages, DeepEquals, []string{string(m1), string(m2)}) |
| 49 |
c.Check(mbox.nids, DeepEquals, []string{"n1", "n2"}) |
| 50 |
} |
| 51 |
|
| 52 |
func (s *mBoxSuite) TestAllMessagesEmpty(c *C) { |
| 53 |
mbox := &mBox{} |
| 54 |
c.Check(mbox.AllMessages(), HasLen, 0) |
| 55 |
} |
| 56 |
|
| 57 |
func (s *mBoxSuite) TestAllMessages(c *C) { |
| 58 |
mbox := &mBox{} |
| 59 |
m1 := json.RawMessage(`{"m":1}`) |
| 60 |
m2 := json.RawMessage(`{"m":2}`) |
| 61 |
mbox.Append(m1, "n1") |
| 62 |
mbox.Append(m2, "n2") |
| 63 |
c.Check(mbox.AllMessages(), DeepEquals, []string{string(m1), string(m2)}) |
| 64 |
} |
| 65 |
|
| 66 |
func blobMessage(n int, sz int) json.RawMessage { |
| 67 |
return json.RawMessage(fmt.Sprintf(`{"n":%d,"b":"%s"}`, n, strings.Repeat("x", sz-14))) |
| 68 |
} |
| 69 |
|
| 70 |
func (s *mBoxSuite) TestAppendEvictSome(c *C) { |
| 71 |
mbox := &mBox{} |
| 72 |
m1 := blobMessage(1, 25) |
| 73 |
m2 := blobMessage(2, 25) |
| 74 |
m3 := blobMessage(3, 50) |
| 75 |
mbox.Append(m1, "n1") |
| 76 |
mbox.Append(m2, "n2") |
| 77 |
mbox.Append(m3, "n3") |
| 78 |
c.Check(mbox.curSize, Equals, 100) |
| 79 |
c.Check(mbox.evicted, Equals, 0) |
| 80 |
m4 := blobMessage(4, 23) |
| 81 |
mbox.Append(m4, "n4") |
| 82 |
c.Assert(mbox.evicted, Equals, 1) |
| 83 |
c.Check(mbox.curSize, Equals, 25+50+23) |
| 84 |
c.Check(mbox.AllMessages(), DeepEquals, []string{string(m2), string(m3), string(m4)}) |
| 85 |
c.Check(mbox.messages[:1], DeepEquals, []string{""}) |
| 86 |
c.Check(mbox.nids, DeepEquals, []string{"", "n2", "n3", "n4"}) |
| 87 |
} |
| 88 |
|
| 89 |
func (s *mBoxSuite) TestAppendEvictSomeCopyOver(c *C) { |
| 90 |
mbox := &mBox{} |
| 91 |
m1 := blobMessage(1, 25) |
| 92 |
m2 := blobMessage(2, 25) |
| 93 |
m3 := blobMessage(3, 25) |
| 94 |
m4 := blobMessage(4, 25) |
| 95 |
mbox.Append(m1, "n1") |
| 96 |
mbox.Append(m2, "n2") |
| 97 |
mbox.Append(m3, "n3") |
| 98 |
mbox.Append(m4, "n4") |
| 99 |
c.Check(mbox.curSize, Equals, 100) |
| 100 |
c.Check(mbox.evicted, Equals, 0) |
| 101 |
m5 := blobMessage(5, 40) |
| 102 |
mbox.Append(m5, "n5") |
| 103 |
c.Assert(mbox.evicted, Equals, 0) |
| 104 |
c.Check(mbox.curSize, Equals, 90) |
| 105 |
c.Check(mbox.AllMessages(), DeepEquals, []string{string(m3), string(m4), string(m5)}) |
| 106 |
c.Check(mbox.nids, DeepEquals, []string{"n3", "n4", "n5"}) |
| 107 |
// do it again |
| 108 |
m6 := blobMessage(6, 40) |
| 109 |
mbox.Append(m6, "n6") |
| 110 |
c.Assert(mbox.evicted, Equals, 0) |
| 111 |
c.Check(mbox.curSize, Equals, 80) |
| 112 |
c.Check(mbox.AllMessages(), DeepEquals, []string{string(m5), string(m6)}) |
| 113 |
c.Check(mbox.nids, DeepEquals, []string{"n5", "n6"}) |
| 114 |
} |
| 115 |
|
| 116 |
func (s *mBoxSuite) TestAppendEvictEverything(c *C) { |
| 117 |
mbox := &mBox{} |
| 118 |
m1 := blobMessage(1, 25) |
| 119 |
m2 := blobMessage(2, 25) |
| 120 |
m3 := blobMessage(3, 50) |
| 121 |
mbox.Append(m1, "n1") |
| 122 |
mbox.Append(m2, "n2") |
| 123 |
mbox.Append(m3, "n3") |
| 124 |
c.Check(mbox.curSize, Equals, 100) |
| 125 |
c.Check(mbox.evicted, Equals, 0) |
| 126 |
m4 := blobMessage(4, 90) |
| 127 |
mbox.Append(m4, "n4") |
| 128 |
c.Assert(mbox.evicted, Equals, 0) |
| 129 |
c.Check(mbox.curSize, Equals, 90) |
| 130 |
c.Check(mbox.AllMessages(), DeepEquals, []string{string(m4)}) |
| 131 |
c.Check(mbox.nids, DeepEquals, []string{"n4"}) |
| 132 |
} |