Ejemplo n.º 1
0
        public void LiveNodes_LastUpdateMoreThanExpiryInterval_ReturnsZero()
        {
            // Arrange
            var stubRandomGenerator = new Mock <IRandomGenerator>();

            stubRandomGenerator.Setup(x => x.Generate(It.IsAny <int>())).Returns(0);
            var stubTimeProvider = new Mock <ITimeProvider>();
            var sut = new DataNodeRepository(stubRandomGenerator.Object, stubTimeProvider.Object);
            var now = DateTime.Now;

            stubTimeProvider.Setup(x => x.Now).Returns(now);
            var dataNodeId = new DataNodeId()
            {
                IPAddress = "IPAddress",
                HostName  = "HostName",
            };

            sut.AddDataNode(dataNodeId);

            stubTimeProvider.Setup(x => x.Now).Returns(now.AddMilliseconds(sut.HeartBeatExpireIntervalMilliseconds + 1));

            // Act
            var result = sut.LiveNodes;

            // Assert
            Assert.AreEqual(0, result);
        }
Ejemplo n.º 2
0
        public LocatedBlock AddBlock(string srcFile)
        {
            var randomDataNodeID   = _dataNodeRepository.GetRandomDataNodeId();
            var dataNodeDescriptor = _dataNodeRepository.GetDataNodeDescriptorById(randomDataNodeID);

            var dataNodeID = new DataNodeId {
                HostName = dataNodeDescriptor.HostName, IPAddress = dataNodeDescriptor.IPAddress
            };
            var dataNodeIds = new List <DataNodeId> {
                dataNodeID
            };

            var blockId = Guid.NewGuid();
            var block   = new Block(blockId, 0, DateTime.Now);

            var blockInfo = new BlockInfo(block, dataNodeIds);

            var node = _fileSystem.GetFile(srcFile);

            node.AddBlock(blockInfo);

            var locatedBlock = new LocatedBlock
            {
                Block     = block,
                Locations = dataNodeIds
            };

            return(locatedBlock);
        }
Ejemplo n.º 3
0
        public void Run(string url)
        {
            var dataNodeId = new DataNodeId {
                IPAddress = url, HostName = Dns.GetHostName()
            };

            _nameNode.BaseUrl = new Uri(_dataNodeOptions.NameNodeUri);
            ID = _nameNode.RegisterDataNode(dataNodeId);

            var timer = new Timer(1000);

            timer.Elapsed += new ElapsedEventHandler(SendHeartbeat);
            timer.Start();
        }
        public void Register_Always_InvokesRegister()
        {
            // Arrange
            var mockDataNodeProtocol = new Mock <IDataNodeProtocol>();
            var dataNodeId           = new DataNodeId();

            mockDataNodeProtocol.Setup(x => x.RegisterDataNode(dataNodeId));
            var controller = new DataNodeProtocolController(mockDataNodeProtocol.Object);

            // Act
            controller.Register(dataNodeId);

            // Assert
            mockDataNodeProtocol.VerifyAll();
        }
Ejemplo n.º 5
0
        public IDataNodeId GetDataNodeDescriptorById(Guid dataNodeGuid)
        {
            // Return a copy of the descriptor to avoid leaky abstraction
            if (_dataNodes.ContainsKey(dataNodeGuid))
            {
                var dataNodeId = _dataNodes[dataNodeGuid].DataNodeId;

                var dataNodeIdCopy = new DataNodeId();
                dataNodeIdCopy.HostName  = dataNodeId.HostName;
                dataNodeIdCopy.IPAddress = dataNodeId.IPAddress;

                return(dataNodeIdCopy);
            }

            return(null);
        }
Ejemplo n.º 6
0
        public void AddDataNode_Always_ReturnsNewDataNodeID()
        {
            // Arrange
            var dataNodeID = new DataNodeId()
            {
                IPAddress = "IPAddress",
                HostName  = "HostName",
            };
            var stubRandomGenerator = new Mock <IRandomGenerator>();

            stubRandomGenerator.Setup(x => x.Generate(It.IsAny <int>())).Returns(0);
            var stubTimeProvider = new Mock <ITimeProvider>();
            var sut = new DataNodeRepository(stubRandomGenerator.Object, stubTimeProvider.Object);

            // Act
            var result = sut.AddDataNode(dataNodeID);

            // Assert
            Assert.IsNotNull(result);
        }
Ejemplo n.º 7
0
        public void GetRandomDataNodeId_Always_ReturnsRandomNodeId()
        {
            // Arrange
            var stubRandomGenerator = new Mock <IRandomGenerator>();

            stubRandomGenerator.Setup(x => x.Generate(It.IsAny <int>())).Returns(0);
            var stubTimeProvider = new Mock <ITimeProvider>();
            var sut        = new DataNodeRepository(stubRandomGenerator.Object, stubTimeProvider.Object);
            var dataNodeId = new DataNodeId()
            {
                IPAddress = "IPAddress",
                HostName  = "HostName",
            };
            var dataNodeGuid = sut.AddDataNode(dataNodeId);

            // Act
            var result = sut.GetRandomDataNodeId();

            // Assert
            Assert.AreEqual(dataNodeGuid, result);
        }
Ejemplo n.º 8
0
        public void RegisterDataNode_Always_PerformsRegisterRequest()
        {
            // Arrange
            var restResponse = new RestResponse <Guid>();

            restResponse.StatusCode = HttpStatusCode.OK;
            restResponse.Data       = Guid.NewGuid();

            var dataNodeId = new DataNodeId();

            var restRequest = new RestRequest("/DataNodeProtocol/Register", Method.POST);

            restRequest.AddJsonBody(dataNodeId);
            _mockRestClient.Setup(x => x.Execute <Guid>(It.IsAny <RestRequest>())).Returns(restResponse);
            var sut = new DataNodeProtocol(_mockRestClient.Object);

            // Act
            var result = sut.RegisterDataNode(dataNodeId);

            // Assert
            Assert.AreEqual(restResponse.Data, result);
            _mockRestClient.Verify(x => x.Execute <Guid>(It.Is <RestRequest>(r => VerifyRestRequest(restRequest, r))));
        }
Ejemplo n.º 9
0
        public void AddBlock_ValidFile_AllocatesBlockAndReturnsLocatedBlock()
        {
            var stubFileSystem = new Mock <IFileSystem>();
            var file           = new File();

            stubFileSystem.Setup(x => x.GetFile(It.IsAny <string>())).Returns(file);

            var stubDataNodeRepository = new Mock <IDataNodeRepository>();
            var dataNodeId             = Guid.NewGuid();

            stubDataNodeRepository.Setup(x => x.GetRandomDataNodeId()).Returns(dataNodeId);
            var dataNodeID = new DataNodeId()
            {
                HostName = "HostName", IPAddress = "IPAddress"
            };

            stubDataNodeRepository.Setup(x => x.GetDataNodeDescriptorById(dataNodeId)).Returns(dataNodeID);

            var clientProtocol = new ClientProtocol(stubFileSystem.Object, stubDataNodeRepository.Object);

            var result = clientProtocol.AddBlock("testFile");

            Assert.That(result.Locations, Has.Exactly(1).Matches <DataNodeId>(x => x.HostName == dataNodeID.HostName && x.IPAddress == dataNodeID.IPAddress));
        }
Ejemplo n.º 10
0
        public void GetDataNodeDescriptorById_ValidId_ReturnsCopyOfDataNodeId()
        {
            // Arrange
            var stubRandomGenerator = new Mock <IRandomGenerator>();

            stubRandomGenerator.Setup(x => x.Generate(It.IsAny <int>())).Returns(0);
            var stubTimeProvider = new Mock <ITimeProvider>();
            var sut        = new DataNodeRepository(stubRandomGenerator.Object, stubTimeProvider.Object);
            var dataNodeId = new DataNodeId()
            {
                IPAddress = "IPAddress",
                HostName  = "HostName",
            };
            var dataNodeGuid = sut.AddDataNode(dataNodeId);

            // Act
            var result = sut.GetDataNodeDescriptorById(dataNodeGuid);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreNotEqual(result, dataNodeId); // Validate result is a copy of the original DataNodeId object
            Assert.AreEqual(dataNodeId.IPAddress, result.IPAddress);
            Assert.AreEqual(dataNodeId.HostName, result.HostName);
        }
Ejemplo n.º 11
0
        public void LiveNodes_LastUpdateLessThanExpiryInterval_ReturnsOne()
        {
            // Arrange
            var stubRandomGenerator = new Mock <IRandomGenerator>();

            stubRandomGenerator.Setup(x => x.Generate(It.IsAny <int>())).Returns(0);
            var stubTimeProvider = new Mock <ITimeProvider>();

            stubTimeProvider.Setup(x => x.Now).Returns(DateTime.Now);
            var sut        = new DataNodeRepository(stubRandomGenerator.Object, stubTimeProvider.Object);
            var dataNodeId = new DataNodeId()
            {
                IPAddress = "IPAddress",
                HostName  = "HostName",
            };

            sut.AddDataNode(dataNodeId);

            // Act
            var result = sut.LiveNodes;

            // Assert
            Assert.AreEqual(1, result);
        }
Ejemplo n.º 12
0
 public Guid Register([FromBody] DataNodeId dataNodeId)
 {
     return(_dataNodeProtocol.RegisterDataNode(dataNodeId));
 }