[#307] v2/reputation/test: Do not allocate memory if !empty

Move all memory allocation and field settings
in `Generate...(empty bool)` functions behind
`if !empty` check.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-11 14:49:52 +03:00 committed by Alex Vanin
parent e459378b16
commit 176be8ead1

View file

@ -21,18 +21,19 @@ func GenerateTrust(empty bool) *reputation.Trust {
if !empty { if !empty {
m.SetValue(1) m.SetValue(1)
m.SetPeer(GeneratePeerID(false))
} }
m.SetPeer(GeneratePeerID(empty))
return m return m
} }
func GeneratePeerToPeerTrust(empty bool) *reputation.PeerToPeerTrust { func GeneratePeerToPeerTrust(empty bool) *reputation.PeerToPeerTrust {
m := new(reputation.PeerToPeerTrust) m := new(reputation.PeerToPeerTrust)
m.SetTrustingPeer(GeneratePeerID(empty)) if !empty {
m.SetTrust(GenerateTrust(empty)) m.SetTrustingPeer(GeneratePeerID(false))
m.SetTrust(GenerateTrust(false))
}
return m return m
} }
@ -40,8 +41,10 @@ func GeneratePeerToPeerTrust(empty bool) *reputation.PeerToPeerTrust {
func GenerateGlobalTrustBody(empty bool) *reputation.GlobalTrustBody { func GenerateGlobalTrustBody(empty bool) *reputation.GlobalTrustBody {
m := new(reputation.GlobalTrustBody) m := new(reputation.GlobalTrustBody)
m.SetManager(GeneratePeerID(empty)) if !empty {
m.SetTrust(GenerateTrust(empty)) m.SetManager(GeneratePeerID(false))
m.SetTrust(GenerateTrust(false))
}
return m return m
} }
@ -49,14 +52,18 @@ func GenerateGlobalTrustBody(empty bool) *reputation.GlobalTrustBody {
func GenerateGlobalTrust(empty bool) *reputation.GlobalTrust { func GenerateGlobalTrust(empty bool) *reputation.GlobalTrust {
m := new(reputation.GlobalTrust) m := new(reputation.GlobalTrust)
m.SetVersion(refstest.GenerateVersion(empty)) if !empty {
m.SetBody(GenerateGlobalTrustBody(empty)) m.SetVersion(refstest.GenerateVersion(false))
m.SetSignature(refstest.GenerateSignature(empty)) m.SetBody(GenerateGlobalTrustBody(false))
m.SetSignature(refstest.GenerateSignature(empty))
}
return m return m
} }
func GenerateTrusts(empty bool) (res []*reputation.Trust) { func GenerateTrusts(empty bool) []*reputation.Trust {
var res []*reputation.Trust
if !empty { if !empty {
res = append(res, res = append(res,
GenerateTrust(false), GenerateTrust(false),
@ -64,7 +71,7 @@ func GenerateTrusts(empty bool) (res []*reputation.Trust) {
) )
} }
return return res
} }
func GenerateAnnounceLocalTrustRequestBody(empty bool) *reputation.AnnounceLocalTrustRequestBody { func GenerateAnnounceLocalTrustRequestBody(empty bool) *reputation.AnnounceLocalTrustRequestBody {
@ -72,19 +79,20 @@ func GenerateAnnounceLocalTrustRequestBody(empty bool) *reputation.AnnounceLocal
if !empty { if !empty {
m.SetEpoch(13) m.SetEpoch(13)
m.SetTrusts(GenerateTrusts(false))
} }
m.SetTrusts(GenerateTrusts(empty))
return m return m
} }
func GenerateAnnounceLocalTrustRequest(empty bool) *reputation.AnnounceLocalTrustRequest { func GenerateAnnounceLocalTrustRequest(empty bool) *reputation.AnnounceLocalTrustRequest {
m := new(reputation.AnnounceLocalTrustRequest) m := new(reputation.AnnounceLocalTrustRequest)
m.SetBody(GenerateAnnounceLocalTrustRequestBody(empty)) if !empty {
m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty)) m.SetBody(GenerateAnnounceLocalTrustRequestBody(false))
m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty)) m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty))
m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty))
}
return m return m
} }
@ -98,9 +106,11 @@ func GenerateAnnounceLocalTrustResponseBody(empty bool) *reputation.AnnounceLoca
func GenerateAnnounceLocalTrustResponse(empty bool) *reputation.AnnounceLocalTrustResponse { func GenerateAnnounceLocalTrustResponse(empty bool) *reputation.AnnounceLocalTrustResponse {
m := new(reputation.AnnounceLocalTrustResponse) m := new(reputation.AnnounceLocalTrustResponse)
m.SetBody(GenerateAnnounceLocalTrustResponseBody(empty)) if !empty {
m.SetMetaHeader(sessiontest.GenerateResponseMetaHeader(empty)) m.SetBody(GenerateAnnounceLocalTrustResponseBody(false))
m.SetVerificationHeader(sessiontest.GenerateResponseVerificationHeader(empty)) m.SetMetaHeader(sessiontest.GenerateResponseMetaHeader(empty))
m.SetVerificationHeader(sessiontest.GenerateResponseVerificationHeader(empty))
}
return m return m
} }
@ -111,7 +121,7 @@ func GenerateAnnounceIntermediateResultRequestBody(empty bool) *reputation.Annou
if !empty { if !empty {
m.SetEpoch(123) m.SetEpoch(123)
m.SetIteration(564) m.SetIteration(564)
m.SetTrust(GeneratePeerToPeerTrust(empty)) m.SetTrust(GeneratePeerToPeerTrust(false))
} }
return m return m
@ -120,7 +130,10 @@ func GenerateAnnounceIntermediateResultRequestBody(empty bool) *reputation.Annou
func GenerateAnnounceIntermediateResultRequest(empty bool) *reputation.AnnounceIntermediateResultRequest { func GenerateAnnounceIntermediateResultRequest(empty bool) *reputation.AnnounceIntermediateResultRequest {
m := new(reputation.AnnounceIntermediateResultRequest) m := new(reputation.AnnounceIntermediateResultRequest)
m.SetBody(GenerateAnnounceIntermediateResultRequestBody(empty)) if !empty {
m.SetBody(GenerateAnnounceIntermediateResultRequestBody(false))
}
m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty)) m.SetMetaHeader(sessiontest.GenerateRequestMetaHeader(empty))
m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty)) m.SetVerificationHeader(sessiontest.GenerateRequestVerificationHeader(empty))
@ -136,7 +149,10 @@ func GenerateAnnounceIntermediateResultResponseBody(empty bool) *reputation.Anno
func GenerateAnnounceIntermediateResultResponse(empty bool) *reputation.AnnounceIntermediateResultResponse { func GenerateAnnounceIntermediateResultResponse(empty bool) *reputation.AnnounceIntermediateResultResponse {
m := new(reputation.AnnounceIntermediateResultResponse) m := new(reputation.AnnounceIntermediateResultResponse)
m.SetBody(GenerateAnnounceIntermediateResultResponseBody(empty)) if !empty {
m.SetBody(GenerateAnnounceIntermediateResultResponseBody(false))
}
m.SetMetaHeader(sessiontest.GenerateResponseMetaHeader(empty)) m.SetMetaHeader(sessiontest.GenerateResponseMetaHeader(empty))
m.SetVerificationHeader(sessiontest.GenerateResponseVerificationHeader(empty)) m.SetVerificationHeader(sessiontest.GenerateResponseVerificationHeader(empty))