using System.Collections.Generic; using Microsoft.Tye.Hosting.Model; using Xunit; namespace Microsoft.Tye.UnitTests { public class ServiceUnitTests { [Theory] [MemberData(nameof(ServiceStateTestData))] public void ServiceStateIsBasedOnReplicaStates(ServiceState expected, List replicaStates) { Service service = new(new ServiceDescription("test", null), ServiceSource.Unknown); for (int i = 0; i < replicaStates.Count; i++) { string replicaName = i.ToString(); service.Replicas.TryAdd(replicaName, new ReplicaStatus(service, replicaName) { State = replicaStates[i], }); } Assert.Equal(expected, service.State); } public static IEnumerable ServiceStateTestData => new List { //no replica - should not happen new object[] { ServiceState.Unknown, new List() }, //one replica new object[] { ServiceState.Starting, new List() { ReplicaState.Added } }, new object[] { ServiceState.Started, new List() { ReplicaState.Started } }, new object[] { ServiceState.Started, new List() { ReplicaState.Ready } }, new object[] { ServiceState.Started, new List() { ReplicaState.Healthy } }, new object[] { ServiceState.Failed, new List() { ReplicaState.Removed } }, new object[] { ServiceState.Stopped, new List() { ReplicaState.Stopped } }, //multiple replicas new object[] { ServiceState.Starting, new List() { ReplicaState.Added, ReplicaState.Started, ReplicaState.Ready, ReplicaState.Healthy } }, new object[] { ServiceState.Started, new List() { ReplicaState.Started, ReplicaState.Ready, ReplicaState.Healthy } }, new object[] { ServiceState.Degraded, new List() { ReplicaState.Removed, ReplicaState.Started, ReplicaState.Ready, ReplicaState.Healthy } }, new object[] { ServiceState.Degraded, new List() { ReplicaState.Stopped, ReplicaState.Started, ReplicaState.Ready, ReplicaState.Healthy } }, new object[] { ServiceState.Degraded, new List() { ReplicaState.Removed, ReplicaState.Stopped, ReplicaState.Started, ReplicaState.Ready, ReplicaState.Healthy } }, new object[] { ServiceState.Stopped, new List() { ReplicaState.Stopped, ReplicaState.Stopped, ReplicaState.Stopped } }, }; } }