Use for writing integration tests and simulating failures. Triggers on: raft test, babuza test, network partition test, fault injection, raft simulation
Package:
github.com/fanaujie/babuza/test/testclusterFramework for orchestrating Babuza integration tests with network failure simulation.
You are an expert at testing distributed systems with babuza. Help users by:
testcluster.Refer to the local files for detailed scenarios:
./references/scenarios.md - Common testing patterns (partition, leadership transfer).Setup a cluster for direct network testing (no faults).
func TestClusterBasic(t *testing.T) {
// 1. Setup
storageDir := t.TempDir()
cluster := testcluster.CreateTestCluster(
1, storageDir, nil, createEmbeddedApp,
)
defer cluster.Teardown()
// 2. Create Peers
peerFactory := testcluster.NewPeerFactory(14200, 10000, 24200)
peers, group := peerFactory.MakeVotingStandardPeers(3)
// 3. Start
wait := cluster.RaftElectionTimeout() * 3
err := cluster.MakeCluster(wait, peers)
require.NoError(t, err)
// 4. Verify Leader
_, err = cluster.CheckOneLeader(wait, group.GetIDs())
require.NoError(t, err)
}
Use ProxyNetwork to simulate split-brain scenarios.
func TestPartition(t *testing.T) {
// 1. Setup with Proxy Network
proxy := proxynetwork.New()
cluster := testcluster.CreateTestCluster(1, dir, proxy, createApp)
// ... start cluster ...
// 2. Isolate Node 3 (Partition: {1,2}, {3})
cluster.SetPartition([]uint64{1, 2})
// 3. Verify Majority works
leader, _ := cluster.CheckOneLeader(wait, []uint64{1, 2})
// 4. Heal Partition
cluster.SetPartition([]uint64{1, 2, 3})
}
Inject latency or packet loss.
import "github.com/fanaujie/babuza/pkg/transport/protocol/tcp/networkio/proxynetwork"
// Add 50ms delay to Node 1
cluster.SetPeerFault(1, proxynetwork.FaultConfig{
DelayMin: 50 * time.Millisecond,
DelayMax: 50 * time.Millisecond,
})
// Add 20% packet loss to Node 2
cluster.SetPeerFault(2, proxynetwork.FaultConfig{
LossRate: 0.2,
})
// Restore
cluster.ClearPeerFault(1)
NewPeerFactory(raftBase, appBase, proxyBase)
raftBase: Start of Raft ports (e.g., 14200)appBase: Start of App Service ports (e.g., 10000)proxyBase: Start of Proxy ports (e.g., 24200)StandardPeer cannot simulate faults; BabuzaPeer (Proxy) is required.cluster.RaftElectionTimeout() * N for waiting, rather than hardcoded sleeps.defer cluster.Teardown().