2020-02-06 04:07:23 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateReferenceSharing_Finalizer(t *testing.T) {
|
|
|
|
// This test showcases the logic on a the RandaoMixes field with the GC finalizer.
|
|
|
|
|
2020-04-14 16:41:09 +00:00
|
|
|
a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{RandaoMixes: [][]byte{[]byte("foo")}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-06 04:07:23 +00:00
|
|
|
if a.sharedFieldReferences[randaoMixes].refs != 1 {
|
|
|
|
t.Error("Expected a single reference for Randao mixes")
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:14:52 +00:00
|
|
|
func() {
|
2020-02-06 04:07:23 +00:00
|
|
|
// Create object in a different scope for GC
|
|
|
|
b := a.Copy()
|
|
|
|
if a.sharedFieldReferences[randaoMixes].refs != 2 {
|
|
|
|
t.Error("Expected 2 references to randao mixes")
|
|
|
|
}
|
|
|
|
_ = b
|
|
|
|
}()
|
|
|
|
|
|
|
|
runtime.GC() // Should run finalizer on object b
|
|
|
|
if a.sharedFieldReferences[randaoMixes].refs != 1 {
|
|
|
|
t.Errorf("Expected 1 shared reference to randao mixes!")
|
|
|
|
}
|
|
|
|
|
|
|
|
b := a.Copy()
|
|
|
|
if b.sharedFieldReferences[randaoMixes].refs != 2 {
|
|
|
|
t.Error("Expected 2 shared references to randao mixes")
|
|
|
|
}
|
2020-04-15 20:35:19 +00:00
|
|
|
if err := b.UpdateRandaoMixesAtIndex(0, []byte("bar")); err != nil {
|
2020-04-14 16:41:09 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-06 04:07:23 +00:00
|
|
|
if b.sharedFieldReferences[randaoMixes].refs != 1 || a.sharedFieldReferences[randaoMixes].refs != 1 {
|
|
|
|
t.Error("Expected 1 shared reference to randao mix for both a and b")
|
|
|
|
}
|
|
|
|
}
|