GCP Engineer: Membangun Testbed Lokal Cloud Datastore & Memcache Hermetik di Luar Google App Engine

Saat memigrasikan layanan Python dari Google App Engine (GAE) Standard ke Google Cloud Run atau Google Kubernetes Engine (GKE), tim rekayasa sering mendapati bahwa rangkaian pengujian mereka masih terikat erat pada runtime lawas google.appengine.ext.testbed. Menjalankan pengujian langsung ke proyek GCP staging bersama menimbulkan kondisi balapan (race condition) antar runner CI dan memperlambat umpan balik pengujian. Sebagai GCP Engineer dan Backend Engineer, saya membangun stub lokal hermetik berbasis emulator untuk Cloud Datastore, Memorystore (Memcache/Redis), dan Cloud Tasks.

1. Fixture Emulator Datastore Terisolasi Namespace untuk Worker Paralel pytest-xdist

Saat menjalankan 8 worker pytest -n auto secara paralel terhadap emulator Cloud Datastore lokal, pengujian akan saling bertabrakan kecuali setiap fungsi uji diisolasi di dalam namespace Datastore yang unik dan dibersihkan secara otomatis setelah selesai:

import os
import uuid
import pytest
from google.cloud import datastore
from google.auth.credentials import AnonymousCredentials

@pytest.fixture
def isolated_datastore_client(monkeypatch: pytest.MonkeyPatch) -> datastore.Client:
    emulator_host = os.environ.get("DATASTORE_EMULATOR_HOST", "localhost:8081")
    monkeypatch.setenv("DATASTORE_EMULATOR_HOST", emulator_host)
    unique_ns = f"test-ns-{uuid.uuid4().hex[:12]}"
    client = datastore.Client(
        project="local-test-project",
        namespace=unique_ns,
        credentials=AnonymousCredentials(),
    )
    yield client
    # Bersihkan seluruh entitas yang dibuat di dalam namespace terisolasi ini
    query = client.query()
    query.keys_only()
    keys = [entity.key for entity in query.fetch()]
    if keys:
        client.delete_multi(keys)

2. Stub Memcache CAS (Compare-And-Swap) In-Memory untuk Pengujian Konkurensi

Beban kerja konkurensi tinggi di GAE dan Cloud Run mengandalkan operasi gets() dan cas() (Compare-And-Swap) pada Memcache untuk mencegah cache stampede. Kami menyediakan stub in-memory aman-thread yang menaikkan penghitung versi CAS 64-bit secara monotonik pada setiap penulisan sehingga pengujian unit dapat mensimulasikan perlombaan penulisan secara deterministik, selaras dengan cetak biru migrasi Cloud Run GCP Engineer dan topologi lokal Docker Compose. Kunjungi Layanan Sprint Teknik 1 Hari untuk modernisasi aplikasi GCP Anda.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs