Scheduled maintenance on Saturday, 27 September 2025, from 07:00 AM to 4:00 PM GMT (09:00 AM to 6:00 PM CEST) - some services may be unavailable -

Skip to content
network_test.go 8.54 KiB
Newer Older
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

	"testing"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)

const networkStoreName string = "network-store"
const networkStoreNamespace string = "network-ns"
const networkStoreInfluxAddr string = "http://localhost:30986"
const networkStoreRedisAddr string = "localhost:30380"

func TestNetworkMetricGetSet(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	fmt.Println("Create valid Metric Store")
	ms, err := NewMetricStore("", networkStoreNamespace, networkStoreInfluxAddr, networkStoreRedisAddr)
	if err != nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	fmt.Println("Invoke API before setting store")
	err = ms.SetCachedNetworkMetric(NetworkMetric{})
	if err == nil {
	}
	_, err = ms.GetCachedNetworkMetric("node1", "node2")
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	if err == nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	}
	err = ms.SetNetworkMetric(NetworkMetric{})
	if err == nil {
	}
	_, err = ms.GetNetworkMetric("node1", "node2", "", 1)
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	if err == nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	}
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	fmt.Println("Set store")
	err = ms.SetStore(networkStoreName, networkStoreNamespace, true)
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	if err != nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	}

	fmt.Println("Flush store metrics")
	ms.Flush()

	// GET/SET CACHED METRICS

	fmt.Println("Get empty cached metric")
	_, err = ms.GetCachedNetworkMetric("node1", "node2")
	if err == nil {
	fmt.Println("Set cached network metric")
	err = ms.SetCachedNetworkMetric(NetworkMetric{"node1", "node2", nil, 0, 0.1, 0, 0.2, 0})
	if err != nil {
	err = ms.SetCachedNetworkMetric(NetworkMetric{"node2", "node1", nil, 1, 1.1, 0, 1.2, 0})
	if err != nil {
	err = ms.SetCachedNetworkMetric(NetworkMetric{"node1", "node2", nil, 2, 2.1, 0, 2.2, 0})
	if err != nil {
	err = ms.SetCachedNetworkMetric(NetworkMetric{"node2", "node1", nil, 3, 3.1, 0, 3.2, 0})
	if err != nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed

	fmt.Println("Get cached network metrics (node1 -> node2)")
	nm, err := ms.GetCachedNetworkMetric("node1", "node2")
	if err != nil {
	if !validateNetworkMetric(nm, 3, 2.1, 3.1, 2.2, 3.2) {

	fmt.Println("Get cached network metrics (node2 -> node1)")
	nm, err = ms.GetCachedNetworkMetric("node2", "node1")
	if err != nil {
	if !validateNetworkMetric(nm, 2, 3.1, 2.1, 3.2, 2.2) {
Simon Pastor's avatar
Simon Pastor committed
	fmt.Println("Get cached network metrics (* -> node1)")
	nmArray, err := ms.GetCachedNetworkMetrics("*", "node1")
	if err != nil {
		t.Fatalf("Failed to get metric")
	}
	if len(nmArray) != 1 {
		t.Fatalf("Did not received the expected number of slices (=1)")
	}
	if !validateNetworkMetric(nmArray[0], 2, 3.1, 2.1, 3.2, 2.2) {
		t.Fatalf("Invalid network metric")
	}

	// GET/SET METRICS

	fmt.Println("Get empty metric")
	nml, err := ms.GetNetworkMetric("node1", "node2", "", 1)
	if err != nil || len(nml) != 0 {

	fmt.Println("Set network metrics")
	err = ms.SetNetworkMetric(NetworkMetric{"node1", "node2", nil, 0, 0.1, 0.2, 0.3, 0.4})
	if err != nil {
	err = ms.SetNetworkMetric(NetworkMetric{"node2", "node1", nil, 1, 1.1, 1.2, 1.3, 1.4})
	if err != nil {
	err = ms.SetNetworkMetric(NetworkMetric{"node1", "node2", nil, 2, 2.1, 2.2, 2.3, 2.4})
	if err != nil {
	err = ms.SetNetworkMetric(NetworkMetric{"node2", "node1", nil, 3, 3.1, 3.2, 3.3, 3.4})
	if err != nil {

	fmt.Println("Get network metrics (node1 -> node2)")
	nml, err = ms.GetNetworkMetric("node1", "node2", "1ms", 0)
	if err != nil || len(nml) != 0 {
		t.Fatalf("No metrics should be found in the last 1 ms")
	nml, err = ms.GetNetworkMetric("node1", "node2", "", 0)
	if err != nil || len(nml) != 2 {
	if !validateNetworkMetric(nml[0], 2, 2.1, 2.2, 2.3, 2.4) {
	if !validateNetworkMetric(nml[1], 0, 0.1, 0.2, 0.3, 0.4) {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	fmt.Println("Get network metrics (node2 -> node1)")
	nml, err = ms.GetNetworkMetric("node2", "node1", "1ms", 0)
	if err != nil || len(nml) != 0 {
		t.Fatalf("No metrics should be found in the last 1 ms")
	nml, err = ms.GetNetworkMetric("node2", "node1", "", 0)
	if err != nil || len(nml) != 2 {
	if !validateNetworkMetric(nml[0], 3, 3.1, 3.2, 3.3, 3.4) {
	if !validateNetworkMetric(nml[1], 1, 1.1, 1.2, 1.3, 1.4) {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
}

func TestNetworkMetricSnapshot(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	// INIT

	fmt.Println("Create valid Metric Store")
	ms, err := NewMetricStore(networkStoreName, networkStoreNamespace, networkStoreInfluxAddr, networkStoreRedisAddr)
	}

	fmt.Println("Flush store metrics")
	ms.Flush()

	// SET CACHED METRICS

	fmt.Println("Get empty cached metric")
	_, err = ms.GetCachedNetworkMetric("node1-0", "node2-0")
	if err == nil {
	}
	_, err = ms.GetCachedNetworkMetric("node2-0", "node1-0")
	if err == nil {
	}

	fmt.Println("Set cached network metric")
	const maxMetricCount = 100
	for i := 0; i < maxMetricCount; i++ {
		err = ms.SetCachedNetworkMetric(NetworkMetric{"node1-" + strconv.Itoa(i), "node2-" + strconv.Itoa(i), nil, 0, 0.1, 0, 0.2, 0})
		if err != nil {
		}
		err = ms.SetCachedNetworkMetric(NetworkMetric{"node2-" + strconv.Itoa(i), "node1-" + strconv.Itoa(i), nil, 1, 1.1, 0, 1.2, 0})
		if err != nil {
		}
	}

	// TAKE SNAPSHOT & GET METRICS

	fmt.Println("Get empty metric")
	nml, err := ms.GetNetworkMetric("node1-0", "node2-0", "", 0)
	if err != nil || len(nml) != 0 {
	}
	nml, err = ms.GetNetworkMetric("node2-0", "node1-0", "", 0)
	if err != nil || len(nml) != 0 {
	}

	fmt.Println("Take snapshot")
	ms.takeNetworkMetricSnapshot()

	fmt.Println("Get network metrics (node1-0 -> node2-0)")
	nml, err = ms.GetNetworkMetric("node1-0", "node2-0", "", 0)
	if err != nil || len(nml) != 1 {
	}
	if !validateNetworkMetric(nml[0], 1, 0.1, 1.1, 0.2, 1.2) {
	}

	fmt.Println("Get network metrics (node2-0 -> node1-0)")
	nml, err = ms.GetNetworkMetric("node2-0", "node1-0", "", 0)
	if err != nil || len(nml) != 1 {
	}
	if !validateNetworkMetric(nml[0], 0, 1.1, 0.1, 1.2, 0.2) {
func validateNetworkMetric(nm NetworkMetric, lat int32, ul float64, dl float64, ulos float64, dlos float64) bool {
	if nm.Lat != lat {
		fmt.Println("nm.Lat[" + strconv.Itoa(int(nm.Lat)) + "] != lat [" + strconv.Itoa(int(lat)) + "]")
		return false
	}
	if nm.UlTput != ul {
		fmt.Println("nm.UlTput[" + fmt.Sprintf("%f", nm.UlTput) + "] != ul [" + fmt.Sprintf("%f", ul) + "]")
		return false
	}
	if nm.DlTput != dl {
		fmt.Println("nm.DlTput[" + fmt.Sprintf("%f", nm.DlTput) + "] != dl [" + fmt.Sprintf("%f", dl) + "]")
		return false
	}
	if nm.UlLoss != ulos {
		fmt.Println("nm.UlLoss[" + fmt.Sprintf("%f", nm.UlLoss) + "] != ulos [" + fmt.Sprintf("%f", ulos) + "]")
		return false
	}
	if nm.DlLoss != dlos {
		fmt.Println("nm.DlLoss[" + fmt.Sprintf("%f", nm.DlLoss) + "] != dlos [" + fmt.Sprintf("%f", dlos) + "]")
Kevin Di Lallo's avatar
Kevin Di Lallo committed
		return false
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	return true