Skip to content

Commit 65d79d8

Browse files
routing: test persist/reset of MC data on disk
Signed-off-by: Mohamed Awnallah <[email protected]>
1 parent 7ab1747 commit 65d79d8

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

routing/missioncontrol_store_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package routing
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"testing"
@@ -293,3 +294,174 @@ func BenchmarkMissionControlStoreFlushing(b *testing.B) {
293294
})
294295
}
295296
}
297+
298+
// timeToUnix converts a time.Time value to its Unix time representation.
299+
func timeToUnix(t time.Time) int64 {
300+
return t.Unix()
301+
}
302+
303+
// timedPairResultsAreEqual compares two TimedPairResult structs for equality.
304+
// It returns true if the FailTime, FailAmt, SuccessTime, and SuccessAmt fields
305+
// of both TimedPairResult structs are equal. The comparison of FailTime and
306+
// SuccessTime is done by converting them to Unix time to avoid issues with
307+
// internal representation differences.
308+
//
309+
// Parameters:
310+
// - tpr1: The first TimedPairResult to compare.
311+
// - tpr2: The second TimedPairResult to compare.
312+
//
313+
// Returns:
314+
// - bool: true if both TimedPairResult structs are equal, false otherwise.
315+
func timedPairResultsAreEqual(tpr1, tpr2 TimedPairResult) bool {
316+
return timeToUnix(tpr1.FailTime) == timeToUnix(tpr2.FailTime) &&
317+
tpr1.FailAmt == tpr2.FailAmt &&
318+
timeToUnix(tpr1.SuccessTime) == timeToUnix(tpr2.SuccessTime) &&
319+
tpr1.SuccessAmt == tpr2.SuccessAmt
320+
}
321+
322+
// TestPersistMCData verifies that the persistMCData function correctly
323+
// stores a given MissionControlSnapshot into the database and retrieves it
324+
// accurately using fetchMCData.
325+
//
326+
// It performs the following steps:
327+
// 1. Creates a test harness for the mission control store.
328+
// 2. Prepares a sample MissionControlSnapshot with timed pair results.
329+
// 3. Persists the snapshot using persistMCData.
330+
// 4. Fetches the stored data using fetchMCData.
331+
// 5. Verifies that the fetched data matches the original snapshot.
332+
func TestPersistMCData(t *testing.T) {
333+
h := newMCStoreTestHarness(t, testMaxRecords, time.Second)
334+
store := h.store
335+
336+
// Prepare a sample mission control snapshot.
337+
snapshot := &MissionControlSnapshot{
338+
Pairs: []MissionControlPairSnapshot{
339+
{
340+
Pair: DirectedNodePair{
341+
From: route.Vertex{1},
342+
To: route.Vertex{2},
343+
},
344+
TimedPairResult: TimedPairResult{
345+
SuccessTime: time.Now().Add(-time.Hour),
346+
SuccessAmt: lnwire.MilliSatoshi(1500),
347+
},
348+
},
349+
{
350+
Pair: DirectedNodePair{
351+
From: route.Vertex{3},
352+
To: route.Vertex{4},
353+
},
354+
TimedPairResult: TimedPairResult{
355+
FailTime: time.Now().Add(-time.Hour),
356+
FailAmt: lnwire.MilliSatoshi(3000),
357+
},
358+
},
359+
},
360+
}
361+
362+
// Persist the mission control snapshot.
363+
err := store.persistMCData(snapshot)
364+
require.NoError(t, err)
365+
366+
// Fetch the data to verify.
367+
mcSnapshots, err := store.fetchMCData()
368+
require.NoError(t, err)
369+
require.Len(t, mcSnapshots, 1)
370+
require.Len(t, mcSnapshots[0].Pairs, 2)
371+
require.Equal(t, snapshot.Pairs[0].Pair, mcSnapshots[0].Pairs[0].Pair)
372+
require.Equal(t, snapshot.Pairs[1].Pair, mcSnapshots[0].Pairs[1].Pair)
373+
require.True(
374+
t, timedPairResultsAreEqual(
375+
snapshot.Pairs[0].TimedPairResult,
376+
mcSnapshots[0].Pairs[0].TimedPairResult,
377+
),
378+
)
379+
require.True(
380+
t, timedPairResultsAreEqual(
381+
snapshot.Pairs[1].TimedPairResult,
382+
mcSnapshots[0].Pairs[1].TimedPairResult,
383+
),
384+
)
385+
}
386+
387+
// TestResetMCData verifies that the resetMCData function correctly
388+
// clears all mission control data from the database.
389+
//
390+
// It performs the following steps:
391+
// 1. Creates a test harness for the mission control store.
392+
// 2. Prepares and persists a sample MissionControlSnapshot.
393+
// 3. Calls resetMCData to clear the mission control data.
394+
// 4. Fetches the data using fetchMCData to verify that it has been reset.
395+
func TestResetMCData(t *testing.T) {
396+
h := newMCStoreTestHarness(t, testMaxRecords, time.Second)
397+
store := h.store
398+
399+
// Prepare a sample mission control snapshot.
400+
snapshot := &MissionControlSnapshot{
401+
Pairs: []MissionControlPairSnapshot{
402+
{
403+
Pair: DirectedNodePair{
404+
From: route.Vertex{1},
405+
To: route.Vertex{2},
406+
},
407+
TimedPairResult: TimedPairResult{
408+
SuccessTime: time.Now().Add(-time.Hour),
409+
SuccessAmt: lnwire.MilliSatoshi(2000),
410+
},
411+
},
412+
},
413+
}
414+
415+
// Persist the mission control snapshot.
416+
err := store.persistMCData(snapshot)
417+
require.NoError(t, err)
418+
419+
// Reset the mission control data.
420+
err = store.resetMCData()
421+
require.NoError(t, err)
422+
423+
// Fetch the data to verify it has been reset.
424+
mcSnapshots, err := store.fetchMCData()
425+
require.NoError(t, err)
426+
require.Len(t, mcSnapshots, 0)
427+
}
428+
429+
// TestDeserializeMCData verifies that the deserializeMCData function correctly
430+
// deserializes key and value bytes into a MissionControlPairSnapshot.
431+
//
432+
// It performs the following steps:
433+
// 1. Prepares sample data for serialization, including 'From' and 'To' public
434+
// keys and a TimedPairResult.
435+
// 2. Serializes the TimedPairResult into JSON format.
436+
// 3. Concatenates the 'From' and 'To' public keys to form the key.
437+
// 4. Deserializes the mission control data using deserializeMCData.
438+
// 5. Verifies that the deserialized data matches the original data.
439+
func TestDeserializeMCData(t *testing.T) {
440+
// Prepare sample data for serialization.
441+
from := route.Vertex{1}
442+
to := route.Vertex{2}
443+
timedPairResult := TimedPairResult{
444+
FailTime: time.Now(),
445+
FailAmt: lnwire.MilliSatoshi(1000),
446+
SuccessTime: time.Now().Add(-time.Hour),
447+
SuccessAmt: lnwire.MilliSatoshi(2000),
448+
}
449+
data, err := json.Marshal(timedPairResult)
450+
require.NoError(t, err)
451+
452+
// Concatenate the 'From' and 'To' public keys to form the key.
453+
// Create the key by concatenating From and To bytes.
454+
key := append([]byte{}, from[:]...)
455+
key = append(key, to[:]...)
456+
457+
// Deserialize the mission control data.
458+
mcSnapshot, err := deserializeMCData(key, data)
459+
require.NoError(t, err)
460+
require.Equal(t, from, mcSnapshot.Pair.From)
461+
require.Equal(t, to, mcSnapshot.Pair.To)
462+
require.True(
463+
t, timedPairResultsAreEqual(
464+
timedPairResult, mcSnapshot.TimedPairResult,
465+
),
466+
)
467+
}

0 commit comments

Comments
 (0)