@@ -37,9 +37,17 @@ type mockValues struct {
37
37
ValidateReturn error
38
38
}
39
39
40
- func (v * mockValues ) ProtoMsg (key string ) (proto.Message , bool ) {
40
+ func (v * mockValues ) Deserialize (key string , value [] byte ) (proto.Message , error ) {
41
41
msg , ok := v .ProtoMsgMap [key ]
42
- return msg , ok
42
+ if ! ok {
43
+ return nil , fmt .Errorf ("Missing message key: %s" , key )
44
+ }
45
+ err := proto .Unmarshal (value , msg )
46
+ if err != nil {
47
+ return nil , err
48
+ }
49
+
50
+ return msg , nil
43
51
}
44
52
45
53
func (v * mockValues ) Validate (interface {}, map [string ]ValueProposer ) error {
@@ -110,39 +118,43 @@ func TestGoodKeys(t *testing.T) {
110
118
mh .AllocateReturn .ProtoMsgMap ["Payload" ] = & cb.Payload {}
111
119
112
120
p := NewProposer (mh )
113
- _ , err := p .BeginValueProposals (t , nil )
121
+ vd , _ , err := p .BeginValueProposals (t , nil )
114
122
assert .NoError (t , err )
115
123
116
124
env := & cb.Envelope {Payload : []byte ("SOME DATA" )}
117
125
pay := & cb.Payload {Data : []byte ("SOME OTHER DATA" )}
118
126
119
- assert .NoError (t , p .ProposeValue (t , "Envelope" , & cb.ConfigValue {Value : utils .MarshalOrPanic (env )}))
120
- assert .NoError (t , p .ProposeValue (t , "Payload" , & cb.ConfigValue {Value : utils .MarshalOrPanic (pay )}))
127
+ msg , err := vd .Deserialize ("Envelope" , utils .MarshalOrPanic (env ))
128
+ assert .NoError (t , err )
129
+ assert .Equal (t , msg , env )
121
130
122
- assert .Equal (t , mh .AllocateReturn .ProtoMsgMap ["Envelope" ], env )
123
- assert .Equal (t , mh .AllocateReturn .ProtoMsgMap ["Payload" ], pay )
131
+ msg , err = vd .Deserialize ("Payload" , utils .MarshalOrPanic (pay ))
132
+ assert .NoError (t , err )
133
+ assert .Equal (t , msg , pay )
124
134
}
125
135
126
136
func TestBadMarshaling (t * testing.T ) {
127
137
mh := newMockHandler ()
128
138
mh .AllocateReturn .ProtoMsgMap ["Envelope" ] = & cb.Envelope {}
129
139
130
140
p := NewProposer (mh )
131
- _ , err := p .BeginValueProposals (t , nil )
141
+ vd , _ , err := p .BeginValueProposals (t , nil )
132
142
assert .NoError (t , err )
133
143
134
- assert .Error (t , p .ProposeValue (t , "Envelope" , & cb.ConfigValue {Value : []byte ("GARBAGE" )}), "Should have errored unmarshaling" )
144
+ _ , err = vd .Deserialize ("Envelope" , []byte ("GARBAGE" ))
145
+ assert .Error (t , err , "Should have errored unmarshaling" )
135
146
}
136
147
137
148
func TestBadMissingMessage (t * testing.T ) {
138
149
mh := newMockHandler ()
139
150
mh .AllocateReturn .ProtoMsgMap ["Payload" ] = & cb.Payload {}
140
151
141
152
p := NewProposer (mh )
142
- _ , err := p .BeginValueProposals (t , nil )
153
+ vd , _ , err := p .BeginValueProposals (t , nil )
143
154
assert .NoError (t , err )
144
155
145
- assert .Error (t , p .ProposeValue (t , "Envelope" , & cb.ConfigValue {Value : utils .MarshalOrPanic (& cb.Envelope {})}), "Should have errored on unexpected message" )
156
+ _ , err = vd .Deserialize ("Envelope" , utils .MarshalOrPanic (& cb.Envelope {}))
157
+ assert .Error (t , err , "Should have errored on unexpected message" )
146
158
}
147
159
148
160
func TestGroups (t * testing.T ) {
@@ -151,18 +163,18 @@ func TestGroups(t *testing.T) {
151
163
mh .NewGroupMap ["bar" ] = nil
152
164
153
165
p := NewProposer (mh )
154
- _ , err := p .BeginValueProposals (t , []string {"foo" , "bar" })
166
+ _ , _ , err := p .BeginValueProposals (t , []string {"foo" , "bar" })
155
167
assert .NoError (t , err , "Both groups were present" )
156
168
p .CommitProposals (t )
157
169
158
170
mh .NewGroupMap = make (map [string ]ValueProposer )
159
- _ , err = p .BeginValueProposals (t , []string {"foo" , "bar" })
171
+ _ , _ , err = p .BeginValueProposals (t , []string {"foo" , "bar" })
160
172
assert .NoError (t , err , "Should not have tried to recreate the groups" )
161
173
p .CommitProposals (t )
162
174
163
- _ , err = p .BeginValueProposals (t , []string {"foo" , "other" })
175
+ _ , _ , err = p .BeginValueProposals (t , []string {"foo" , "other" })
164
176
assert .Error (t , err , "Should not have errored when trying to create 'other'" )
165
177
166
- _ , err = p .BeginValueProposals (t , []string {"foo" })
178
+ _ , _ , err = p .BeginValueProposals (t , []string {"foo" })
167
179
assert .NoError (t , err , "Should be able to begin again without rolling back because of error" )
168
180
}
0 commit comments