Debian Codegraph @

paho/store/memory/store_test.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package memory
17
18 import (
19 "bytes"
20 "errors"
21 "fmt"
22 "testing"
23
24 "github.com/eclipse/paho.golang/packets"
25 )
26
27 // TestMemoryStore basic tests of the memory store
28 func TestMemoryStore(t *testing.T) {
29 s := New()
30
31 ids := []uint16{65535, 2, 10, 32300, 5890}
32 for _, id := range ids {
33 pcp := packets.NewControlPacket(packets.PUBLISH)
34 pcp.Content.(*packets.Publish).PacketID = id
35 pcp.Content.(*packets.Publish).QoS = 1 // ID will only be written for QOS1+
36 pcp.Content.(*packets.Publish).Payload = []byte(fmt.Sprintf("%d", id))
37
38 if err := s.Put(id, packets.PUBLISH, pcp); err != nil {
39 t.Fatalf("failed to put: %s", err)
40 }
41 }
42
43 if err := s.Delete(ids[2]); err != nil {
44 t.Fatalf("failed to delete: %s", err)
45 }
46
47 if _, err := s.Get(8); !errors.Is(err, ErrNotInStore) {
48 t.Fatal("getting missing item should fail")
49 }
50 if _, err := s.Get(ids[2]); !errors.Is(err, ErrNotInStore) {
51 t.Fatal("getting deleted item should fail")
52 }
53 ids = append(ids[:2], ids[3:]...) // keep our record in sync following delete
54
55 if rp, err := s.Get(32300); err != nil {
56 t.Fatalf("failed to get: %s", err)
57 } else {
58 p, err := packets.ReadPacket(rp)
59 if err != nil {
60 t.Fatalf("error decoding packet: %s", err)
61 }
62
63 if p.PacketID() != 32300 {
64 t.Fatalf("unexpected packet id returned: %d", p.PacketID())
65 }
66 payload := p.Content.(*packets.Publish).Payload
67 if bytes.Compare(payload, []byte(fmt.Sprintf("%d", 32300))) != 0 {
68 t.Fatalf("unexpected payload returned: %s", payload)
69 }
70 }
71 rids, err := s.List()
72 if err != nil {
73 t.Fatalf("failed to list: %s", err)
74 }
75 if len(rids) != len(ids) {
76 t.Fatalf("List returned %d elements, expected %d", len(rids), len(ids))
77 }
78 for i, v := range rids {
79 if v != ids[i] {
80 t.Fatalf("List returned %v, expected %v", rids, ids)
81 }
82 }
83
84 s.Reset()
85 rids, err = s.List()
86 if err != nil {
87 t.Fatalf("failed to list: %s", err)
88 }
89 if len(rids) != 0 {
90 t.Fatalf("reset did not clear store: %d", len(rids))
91 }
92
93 }
94
95 // TestMemoryStoreBig creates a fully populated Store and checks things work
96 // Adding messages would make the structure bigger but should have no impact on the struct functions.
97 func TestMemoryStoreBig(t *testing.T) {
98 s := New()
99
100 for id := uint16(1); id != 0; id++ {
101 pcp := packets.NewControlPacket(packets.PUBLISH)
102 pcp.Content.(*packets.Publish).PacketID = id
103 pcp.Content.(*packets.Publish).QoS = 1 // ID will only be written for QOS1+
104 pcp.Content.(*packets.Publish).Payload = []byte(fmt.Sprintf("%d", id))
105
106 if err := s.Put(id, packets.PUBLISH, pcp); err != nil {
107 t.Fatalf("failed to put: %s", err)
108 }
109 }
110
111 for id := uint16(1); id != 0; id++ {
112 rp, err := s.Get(id)
113 if err != nil {
114 t.Fatal("getting missing item should fail")
115 }
116 pcp, err := packets.ReadPacket(rp)
117 if err != nil {
118 t.Fatalf("error decoding packet: %s", err)
119 }
120 sId := pcp.Content.(*packets.Publish).PacketID
121 if id != sId {
122 t.Fatalf("expected %d, gor %d", id, sId)
123 }
124 sPayload := pcp.Content.(*packets.Publish).Payload
125 expPayload := []byte(fmt.Sprintf("%d", id))
126 if bytes.Compare(expPayload, sPayload) != 0 {
127 t.Fatalf("expected %v, got %v", expPayload, sPayload)
128 }
129 }
130 for id := uint16(1); id != 0; id++ {
131 if err := s.Delete(id); err != nil {
132 t.Fatalf("delete failed: %s", err)
133 }
134 }
135 rids, err := s.List()
136 if err != nil {
137 t.Fatalf("failed to list: %s", err)
138 }
139 if len(rids) != 0 {
140 t.Fatalf("everything should have been deleted")
141 }
142 }