File size: 2,419 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Google.Protobuf;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.SideChannels;
using Unity.MLAgents.CommunicatorObjects;


namespace Unity.MLAgents.Tests
{
    /// <summary>
    /// These tests send messages through the event handling code.
    /// There's no output to test, so just make sure there are no exceptions
    /// (and get the code coverage above the minimum).
    /// </summary>
    public class TrainingAnalyticsSideChannelTests
    {
        [Test]
        public void TestTrainingEnvironmentReceived()
        {
            var anyMsg = Google.Protobuf.WellKnownTypes.Any.Pack(new TrainingEnvironmentInitialized());
            var anyMsgBytes = anyMsg.ToByteArray();
            var sideChannel = new TrainingAnalyticsSideChannel();
            using (new AnalyticsUtils.DisableAnalyticsSending())
            {
                sideChannel.ProcessMessage(anyMsgBytes);
            }
        }

        [Test]
        public void TestTrainingBehaviorReceived()
        {
            var anyMsg = Google.Protobuf.WellKnownTypes.Any.Pack(new TrainingBehaviorInitialized());
            var anyMsgBytes = anyMsg.ToByteArray();
            var sideChannel = new TrainingAnalyticsSideChannel();
            using (new AnalyticsUtils.DisableAnalyticsSending())
            {
                sideChannel.ProcessMessage(anyMsgBytes);
            }
        }

        [Test]
        public void TestInvalidProtobufMessage()
        {
            // Test an invalid (non-protobuf) message. This should silently ignore the data.
            var badBytes = Encoding.ASCII.GetBytes("Lorem ipsum");
            var sideChannel = new TrainingAnalyticsSideChannel();
            using (new AnalyticsUtils.DisableAnalyticsSending())
            {
                sideChannel.ProcessMessage(badBytes);
            }

            // Test an almost-valid message. This should silently ignore the data.
            var anyMsg = Google.Protobuf.WellKnownTypes.Any.Pack(new TrainingBehaviorInitialized());
            var anyMsgBytes = anyMsg.ToByteArray();
            var truncatedMessage = new ArraySegment<byte>(anyMsgBytes, 0, anyMsgBytes.Length - 1).ToArray();
            using (new AnalyticsUtils.DisableAnalyticsSending())
            {
                sideChannel.ProcessMessage(truncatedMessage);
            }
        }
    }
}