Ctrl K
ComunidadGuías…

pkg/models/mock_shallowcopy_test.go

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
package models

import (
	"reflect"
	"testing"
	"time"
)

// ShallowCopy is how the replay mock manager stages a mock again once a pool
// holds it: the copy must carry everything but the pooled mark, which belongs
// to the object a pool stored.
func TestShallowCopyIsAnUnpooledCopy(t *testing.T) {
	m := &Mock{
		Version: "api.keploy.io/v1beta1",
		Name:    "m",
		Kind:    MySQL,
		Spec: MockSpec{
			Metadata:         map[string]string{"type": "mocks"},
			ReqTimestampMock: time.Unix(1, 0),
		},
		TestModeInfo: TestModeInfo{ID: 3, IsFiltered: true, SortOrder: 7, Lifetime: LifetimeSession,
			LifetimeDerived: true, IsStartup: true},
		ConnectionID: "c1",
		SourcePID:    42,
		Noise:        []string{"x"},
		Origin:       &Origin{Clock: "proxy:run-1", Comm: "sqs", Exe: "./sqs"},
	}
	m.SetResponseHydrator(func() (*HTTPResp, []MongoResponse, error) { return nil, nil, nil })
	m.onDisk = &payloadOnDisk{at: PayloadAt{Offset: 5, Length: 9, Name: "m"}}
	m.MarkPooled()

	m.SetStoredSize(4096)
	c := m.ShallowCopy()
	if c == m {
		t.Fatal("ShallowCopy returned the same object")
	}
	if c.Pooled() {
		t.Error("the copy is marked pooled; it is a new object no pool holds")
	}
	if !m.Pooled() {
		t.Error("copying cleared the original's pooled mark")
	}
	if !c.HasSpilledResponse() {
		t.Error("the response hydrator was not carried over")
	}
	if c.onDisk != m.onDisk {
		t.Error("where the payload was left on disk was not carried over")
	}
	if c.StoredSize() != 4096 {
		t.Error("the store's count of the mock's bytes was not carried over")
	}
	cv, mv := reflect.ValueOf(*c), reflect.ValueOf(*m)
	for i := 0; i < cv.NumField(); i++ {
		switch name := cv.Type().Field(i).Name; name {
		case "pooled", "responseHydrator", "onDisk", "storedSize": // checked above
		default:
			if !reflect.DeepEqual(cv.Field(i).Interface(), mv.Field(i).Interface()) {
				t.Errorf("field %s was not copied", name)
			}
		}
	}
	c.TestModeInfo.ID = 99
	if m.TestModeInfo.ID != 3 {
		t.Error("the copy shares TestModeInfo with the original")
	}
}

// DeepCopy is how the replay's filters copy each mock before the mock
// manager holds it: the copy carries the store's count of the mock's bytes
// (StoredSize), so the learn step's readers reserve what reading the copy's
// payload costs, and not all their bytes in flight, as for a mock no store
// counted whose answer is parked.
func TestDeepCopyKeepsTheStoresCountOfTheMocksBytes(t *testing.T) {
	m := &Mock{Kind: HTTP, Name: "m"}
	m.SetStoredSize(4096)
	if c := m.DeepCopy(); c.StoredSize() != 4096 {
		t.Fatalf("the copy's stored size is %d: want the mock's 4096", c.StoredSize())
	}
	if c := (&Mock{Kind: HTTP}).DeepCopy(); c.StoredSize() != 0 {
		t.Fatalf("a copy of a mock no store read counts %d bytes: want 0", c.StoredSize())
	}
}