Giter Site home page Giter Site logo

mongo2go / mongo2go Goto Github PK

View Code? Open in Web Editor NEW
373.0 20.0 69.0 473.56 MB

Mongo2Go - MongoDB for .NET integration tests

License: MIT License

C# 99.54% Batchfile 0.22% Shell 0.25%
c-sharp mongodb mongoexport mongoimport dotnet dotnet-core dotnet-standard

mongo2go's Introduction

Mongo2Go - MongoDB for integration tests & local debugging

Logo

Continuous Integration NuGet

👋 Do you need MongoDB 5 or even 6 for your testing or development needs? Check out EphemeralMongo, a project based on Mongo2Go. In addition to supporting multiple MongoDB major versions and the same features, it has a faster startup, and better CI support.

Mongo2Go is a managed wrapper around the latest MongoDB binaries. It targets .NET Standard 2.0 (and .NET 4.6 for legacy environments) and works with Windows, Linux and macOS. This Nuget package contains the executables of mongod, mongoimport and mongoexport for Windows, Linux and macOS .

Brought to you by Johannes Hoppe and Cédric Luthi.

Mongo2Go has two use cases:

  1. Providing multiple, temporary and isolated MongoDB databases for unit tests (or to be precise: integration tests)
  2. Providing a quick to set up MongoDB database for a local developer environment

Unit Test / Integration test

With each call of the static method MongoDbRunner.Start() a new MongoDB instance will be set up. A free port will be used (starting with port 27018) and a corresponding data directory will be created. The method returns an instance of MongoDbRunner, which implements IDisposable. As soon as the MongoDbRunner is disposed (or if the Finalizer is called by the GC), the wrapped MongoDB process will be killed and all data in the data directory will be deleted.

Local debugging

In this mode a single MongoDB instance will be started on the default port (27017). No data will be deleted and the MongoDB instance won’t be killed automatically. Multiple calls to MongoDbRunner.StartForDebugging() will return an instance with the State “AlreadyRunning”. You can ignore the IDisposable interface, as it won’t have any effect. I highly recommend to not use this mode on productive machines! Here you should set up a MongoDB as it is described in the manual. For you convenience the MongoDbRunner also exposes mongoexport and mongoimport which allow you to quickly set up a working environment.

Single server replica set mode to enable transactions

MongoDbRunner.Start() can be set up to take in an optional boolean parameter called singleNodeReplSet. When passed in with the value true - (MongoDbRunner.Start(singleNodeReplSet: true))

  • a single node mongod instance will be started as a replica set with the name singleNodeReplSet. Replica set mode is required for transactions to work in MongoDB 4.0 or greater

Replica set initialization requires the use of a short delay to allow for the replica set to stabilize. This delay is linked to a timeout value of 5 seconds.

If the timeout expires before the replica set has stabilized a TimeoutException will be thrown.

The default timeout can be changed through the optional parameter singleNodeReplSetWaitTimeout, which allows values between 0 and 65535 seconds: MongoDbRunner.Start(singleNodeReplSet: true, singleNodeReplSetWaitTimeout: 10)

Additional mongod arguments

MongoDbRunner.Start() can be set up to consume additional mongod arguments. This can be done using the string parameter called additionalMongodArguments.

The list of additional arguments cannot contain arguments already defined internally by Mongo2Go. An ArgumentException will be thrown in this case, specifying which additional arguments are required to be discarded.

Example of usage of the additional mongod arguments: MongoDbRunner.Start(additionalMongodArguments: "--quiet")

Installation

The Mongo2Go Nuget package can be found at https://nuget.org/packages/Mongo2Go/

Search for „Mongo2Go“ in the Manage NuGet Packages dialog box or run:

PM> Install-Package Mongo2Go

or run for the deprecated .NET Standard 1.6 package:

PM> Install-Package Mongo2Go -Version 2.2.16

or run for the legacy .NET 4.6 package:

PM> Install-Package Mongo2Go -Version 1.1.0

in the Package Manager Console.

  • The new 3.x branch targets .NET Standard 2.0. Please use this version if possible.
  • The old 2.x branch targets .NET Standard 1.6. No new features will be added, only bugfixes might be made.
  • The old 1.x branch targets good-old classic .NET 4.6.1. This is for legacy environments only. No changes will be made.

Examples

Example: Integration Test (here: Machine.Specifications & Fluent Assertions)

[Subject("Runner Integration Test")]
public class when_using_the_inbuild_serialization : MongoIntegrationTest
{
    static TestDocument findResult;
    
    Establish context = () =>
        {
            CreateConnection();
            _collection.Insert(TestDocument.DummyData1());
        };

    Because of = () => findResult = _collection.FindOneAs<TestDocument>();

    It should_return_a_result = () => findResult.ShouldNotBeNull();
    It should_hava_expected_data = () => findResult.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(TestDocument.DummyData1());

    Cleanup stuff = () => _runner.Dispose();
}

public class MongoIntegrationTest
{
    internal static MongoDbRunner _runner;
    internal static MongoCollection<TestDocument> _collection;

    internal static void CreateConnection()
    {
        _runner = MongoDbRunner.Start();
        
        MongoClient client = new MongoClient(_runner.ConnectionString);
        MongoDatabase database = client.GetDatabase("IntegrationTest");
        _collection = database.GetCollection<TestDocument>("TestCollection");
    }
}    

More tests can be found at https://github.com/Mongo2Go/Mongo2Go/tree/master/src/Mongo2GoTests/Runner

Example: Exporting seed data

using (MongoDbRunner runner = MongoDbRunner.StartForDebugging()) {

    runner.Export("TestDatabase", "TestCollection", @"..\..\App_Data\test.json");
}

Example: Importing for local debugging (compatible with ASP.NET MVC 4 Web API as well as ASP.NET Core)

public class WebApiApplication : System.Web.HttpApplication
{
    private MongoDbRunner _runner;

    protected void Application_Start()
    {
        _runner = MongoDbRunner.StartForDebugging();
        _runner.Import("TestDatabase", "TestCollection", @"..\..\App_Data\test.json", true);

        MongoClient client = new MongoClient(_runner.ConnectionString);
        MongoDatabase database = client.GetDatabase("TestDatabase");
        MongoCollection<TestObject> collection = database.GetCollection<TestObject>("TestCollection");

        /* happy coding! */
    }

    protected void Application_End()
    {
        _runner.Dispose();
    }
}

Example: Transactions (New feature since v2.2.8)

Full integration test with transaction handling (click to show)
 public class when_transaction_completes : MongoTransactionTest
    {
        private static TestDocument mainDocument;
        private static TestDocument dependentDocument;
        Establish context = () =>

        {
            _runner = MongoDbRunner.Start(singleNodeReplSet: true);
             client = new MongoClient(_runner.ConnectionString);
            database = client.GetDatabase(_databaseName);
            _mainCollection = database.GetCollection<TestDocument>(_mainCollectionName);
            _dependentCollection = database.GetCollection<TestDocument>(_dependentCollectionName);
            _mainCollection.InsertOne(TestDocument.DummyData2());
            _dependentCollection.InsertOne(TestDocument.DummyData2());
        };

        private Because of = () =>
        {
            var filter = Builders<TestDocument>.Filter.Where(x => x.IntTest == 23);
            var update = Builders<TestDocument>.Update.Inc(i => i.IntTest, 10);

            using (var sessionHandle = client.StartSession())
            {
                try
                {
                    var i = 0;
                    while (i < 10)
                    {
                        try
                        {
                            i++;
                            sessionHandle.StartTransaction(new TransactionOptions(
                                readConcern: ReadConcern.Local,
                                writeConcern: WriteConcern.W1)); 
                            try
                            {
                                var first = _mainCollection.UpdateOne(sessionHandle, filter, update);
                                var second = _dependentCollection.UpdateOne(sessionHandle, filter, update);
                            }
                            catch (Exception e)
                            {
                                sessionHandle.AbortTransaction();
                                throw;
                            }

                            var j = 0;
                            while (j < 10)
                            {
                                try
                                {
                                    j++;
                                    sessionHandle.CommitTransaction();
                                    break;
                                }
                                catch (MongoException e)
                                {
                                    if (e.HasErrorLabel("UnknownTransactionCommitResult"))
                                        continue;
                                    throw;
                                }
                            }
                            break;
                        }
                        catch (MongoException e)
                        {
                            if (e.HasErrorLabel("TransientTransactionError"))
                                continue;
                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    //failed after multiple attempts so log and do what is appropriate in your case
                }
            }

             mainDocument = _mainCollection.FindSync(Builders<TestDocument>.Filter.Empty).FirstOrDefault();
             dependentDocument = _dependentCollection.FindSync(Builders<TestDocument>.Filter.Empty).FirstOrDefault();
        };
        
        It main_should_be_33 = () => mainDocument.IntTest.Should().Be(33);
        It dependent_should_be_33 = () => dependentDocument.IntTest.Should().Be(33);
        Cleanup cleanup = () => _runner.Dispose();
    }

Example: Logging with ILogger

Wire mongod's logs at info and above levels to a custom `ILogger` (click to show)
public class MongoIntegrationTest
{
    internal static MongoDbRunner _runner;

    internal static void CreateConnection()
    {
        // Create a custom logger. 
        // Replace this code with your own configuration of an ILogger.
        var provider = new ServiceCollection()
            .AddLogging(config =>
            {
                // Log to a simple console and to event logs.
                config.AddSimpleConsole();
                config.AddEventLog();
            })
            .BuildServiceProvider();
        var logger = provider.GetSerivce<ILoggerFactory>().CreateLogger("Mongo2Go");

        _runner = MongoDbRunner.Start(logger: logger);
    }
}    
Wire mongod's logs at debug levels to a custom `ILogger` (click to show)
public class MongoIntegrationTest
{
    internal static MongoDbRunner _runner;

    internal static void CreateConnection()
    {
        // Create a custom logger. 
        // Replace this code with your own configuration of an ILogger.
        var provider = new ServiceCollection()
            .AddLogging(config =>
            {
                // Mongod's D1-D2 levels are logged with Debug level.
                // D3-D5 levels are logged with Trace level.
                config.SetMinimumLevel(LogLevel.Trace);

                // Log to System.Diagnostics.Debug and to the event source.
                config.AddDebug();
                config.AddEventSourceLogger();
            })
            .BuildServiceProvider();
        var logger = provider.GetSerivce<ILoggerFactory>().CreateLogger("Mongo2Go");

        _runner = MongoDbRunner.Start(
            additionalMongodArguments: "vvvvv", // Tell mongod to output its D5 level logs
            logger: logger);
    }
}    

Changelog

Mongo2Go 3.1.3, April 30 2021

  • targeting .NET Standard 2.0 instead of 2.1, this makes Mongo2Go compatible with .NET Framework (version 4.7.1 and later) (PR #118 - many thanks to Cédric Luthi)
  • fixes handling of the path search for the NUGET_PACKAGE environment variable (PR #119 - many thanks to Timm Hoffmeister)
  • internal: dotnet pack is now used to create the nupkg file for a release (PR #121 - many thanks to Cédric Luthi)

Mongo2Go 3.1.1, April 08 2021

  • internal: Better algorithm for determining a free port. This allows parallel execution of tests and increases compatibility with Raider and other test runners. (PR #116, fixes #115 and #106 - many thanks to liangshiwei)

Mongo2Go 3.1.0, April 07 2021

  • NEW: Configurable logging! adds the option to inject a Microsoft.Extensions.Logging.ILogger to MongoDbRunner.Start(logger) arguments. Now you can adjust or disable the console output to avoid noise in CI environments. Please note the two examples shown above. (PR #113, fixes #94, #95 and #113 - many thanks to Corentin Altepe)
  • internal: replaces --sslMode disabled (deprecated) with --tlsMode disabled in command line arguments to mongod.

Mongo2Go 3.0.0, March 26 2021

  • includes MongoDB binaries of version 4.4.4 with support for Windows, Linux and macOS

  • targets .NET Standard 2.1 (can be used with .NET Core 3.0 and .NET 5.0)

  • adds new MongoDownloader tool (PR #109, fixes #82 and #112 - many thanks to Cédric Luthi)

  • adds support for NUGET_PACKAGES environment variable (PR #110 - many thanks to Bastian Eicher)

Changelog v2.0.0-alpha1 to v2.2.16 (click to show)

Mongo2Go 2.2.16, December 13 2020

  • fix for non existing starting path for binary search (PR #107, fixes #105 - many thanks to Gurov Yury)

Mongo2Go 2.2.15, December 12 2020

  • throw exception if cluster is not ready for transactions after singleNodeReplSetWaitTimeout (PR #103 - many thanks for the continued support by José Mira) s

Mongo2Go 2.2.14, October 17 2020

  • fixes a bug with pulling mongo binaries from wrong version (PR #87, fixes #86 - many thanks to mihevc)
  • ensures transaction is ready (solves error message: System.NotSupportedException : StartTransaction cannot determine if transactions are supported because there are no connected servers.) (PR #101, fixes #89, #91 and #100 - many thanks to liangshiwei)

Mongo2Go 2.2.12, September 07 2019

  • performance: waits for replica set ready log message, or throws if timeout expires, instead of using Thread.Sleep(5000) (PR #83, fixes #80 - many thanks again to José Mira)

Mongo2Go 2.2.11, May 10 2019

  • allows additional custom MongoDB arguments (PR #69, fixes #68 - many thanks to José Mira)
  • adds option to set port for StartForDebugging() (PR #72, fixes #71 - many thanks to Danny Bies)

Mongo2Go 2.2.9, February 04 2019

  • fixes a file path issue on Linux if you run on an SDK version beyond .NET Standard 1.6 (PR #63, fixes #62 and #61) - many thanks to Jeroen Vannevel)
  • continuous integration runs on Linux (Travis CI) and Windows (AppVeyor) now

Mongo2Go 2.2.8, October 12 2018

  • updated MongoDB binaries to 4.0.2 to support tests leveraging transaction across different collections and databases
  • updated MongoDB C# driver to 2.7.0 to be compatible with MongoDB 4.0
  • adds singleNodeReplSet paramter to MongoDbRunner.Start which allows mongod instance to be started as a replica set to enable transaction support (PR #57 - many thanks to Mahi Satyanarayana)
  • fixes port lookup for UnixPortWatcher (PR #58 - many thanks to Viktor Kolybaba)

Mongo2Go 2.2.7, August 13 2018

  • updates the MongoBinaryLocator to look for binaries in the nuget cache if they are not found in the project directory.
    • this will make Mongo2Go compatible with projects using the nuget PackageReference option. (PR #56, fixes #39 and #55)
  • adds the binariesSearchDirectory parameter to MongoDbRunner.Start which allows an additional binaries search directory to be provided.
    • this will make the db runner more flexible if someone decides to use it in some unpredictable way.
  • many thanks to Nicholas Markkula

Mongo2Go 2.2.6, July 20 2018

  • fixes broken linux support (fixes #47)

Mongo2Go 2.2.5, July 19 2018

Mongo2Go 2.2.4, June 06 2018

  • better support for TeamCity: removed MaxLevelOfRecursion limitation when searching for MongoDb binaries (PR #50, fixes #39)
  • many thanks to Stanko Culaja

Mongo2Go 2.2.2, June 05 2018

  • includes mongod, mongoimport and mongoexport v3.6.1 for Windows, Linux and macOS via PR #46, which fixes #45
  • many thanks to Joe Chan

Mongo2Go 2.2.1, November 23 2017

  • no MongoDB binaries changed, still .NET Standard 1.6
  • feature: uses temporary directory instead of good-old windows style C:\data\db by default (PR #42) - MongoDbRunner.Start() and MongoDbRunner.StartForDebugging() will now work without any extra parameters for Linux/macOS
  • bugfix: runs again on Linux/macOS, by making the binaries executable (PR #42, which fixes #37 and might also fix #43)
  • internal: Unit Tests are running again (PR #44, which fixes #31, #40)
  • internal: No hardcoded path passed to MongoDbRunner constructor (fixes 41)
  • many thanks to Per Liedman

Mongo2Go 2.2.0, August 17 2017

  • includes mongod, mongoimport and mongoexport v3.4.7 for Windows, Linux and macOS
  • targets .NET Standard 1.6 (can be used with .NET Core 1.0 / 1.1 / 2.0)
  • many thanks to Aviram Fireberger

Mongo2Go 2.1.0, March 10 2017

  • skips v2.0 to have same numbers as v1.x.
  • no MongoDB binaries changed since 2.0.0-alpha1 (still MongoDB v3.2.7 for Windows, Linux and macOS)
  • targets .NET Standard 1.6 (can be used with .NET Core 1.0 / 1.1)
  • bugfix: prevent windows firewall popup (PR #30, which fixes #21)
  • many thanks to kubal5003

Mongo2Go 1.1.0, March 10 2017 (legacy branch!)

  • no MongoDB binaries changed since v1.0 (still MongoDB v3.2.7 for Windows, Linux and macOS)
  • targets .NET 4.6.1
  • bugfix: prevent windows firewall popup (PR #29, which fixes #21)
  • many thanks to kubal5003

Mongo2Go 2.0.0-alpha1, December 19 2016

  • this version has no support for .NET Framework 4.6, please continue to use the stable package v.1.0.0
  • NEW: first support of .NET Standard 1.6 (#25)
    • many thanks to Hassaan Ahmed
    • see the Wiki for more information about .NET Core 1.0 / .NET Standard 1.6
Changelog v0.1.0 to v1.0.0 (click to show)

Mongo2Go 1.0.0, November 14 2016

  • v1.0 finally marked as stable
  • no changes to 1.0.0-beta4
  • changes since last stable version (0.2):
    • includes mongod, mongoimport and mongoexport v3.2.7 for Windows, Linux and macOS
    • support for Windows, Linux and macOS
    • uses MongoDB.Driver 2.3.0
    • requires .NET 4.6
    • various small bugfixes and improvements

Mongo2Go 1.0.0-beta4, October 24 2016

  • update to MongoDB.Driver 2.3.0 (#23)
  • upgraded to .NET 4.6
  • internal change: update MSpec as well and add MSTest Adapter for MSpec (ReSharper console runner doesn't support 4.6)
  • many thanks to Alexander Zeitler
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta3, August 22 2016

  • feature: process windows are hidden now (#20)
  • bugfix: random folders are used for storing databases (#18)
  • many thanks to Matt Kocaj
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta2, July 29 2016

  • fixes for bugs that were introduced by the big rewrite for cross-platform support
  • changes from pull request #14, which fixes #12, #13 and #15, many thanks to Mitch Ferrer
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta, July 24 2016

  • 🎉 NEW: support for Linux and macOS 🎉
  • many thanks to Kristofer Linnestjerna from netclean.com for the new cross-platform support
  • includes mongod, mongoimport and mongoexport v3.2.7 for Windows, Linux and macOS
  • changes from pull request #8, #10, #11 which fixes #9
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 0.2, May 30 2016

Mongo2Go 0.1.8, March 13 2016

  • includes mongod, mongoimport and mongoexport v3.0.10 (32bit)
  • changes from pull request #5, thanks to Aristarkh Zagorodnikov

Mongo2Go 0.1.6, July 21 2015

  • includes mongod, mongoimport and mongoexport v3.0.4 (32bit)
  • bug fix #4:
    Sometimes the runner tries to delete the database directory before the mongod process has been stopped, this throws an IOException. Now the runner waits until the mongod process has been stopped before the database directory will be deleted.
  • Thanks Sergey Zwezdin

Mongo2Go 0.1.5, July 08 2015

  • includes mongod, mongoimport and mongoexport v2.6.6 (32bit)
  • changes from pull request #3
  • new: Start and StartForDebugging methods accept an optional parameter to specify a different data directory (default is "C:\data\db")
  • many thanks to Marc

Mongo2Go 0.1.4, January 26 2015

  • includes mongod, mongoimport and mongoexport v2.6.6 (32bit)
  • changes from pull request #2
  • internal updates for testing the package (not part of the release)
    • updated MSpec package so that it would work with the latest VS and R# test runner
    • updated Mongo C# Driver, Fluent Assertions, and Moq packages to latest versions
    • fixed date handling for mongoimport and mongoexport to pass tests
  • many thanks to Jesse Sweetland

Mongo2Go 0.1.3, September 20 2012

  • includes mongod, mongoimport and mongoexport v2.2.0 (32bit)

Mongo2Go 0.1.2, August 20 2012

  • stable version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

Mongo2Go 0.1.1, August 16 2012

  • second alpha version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

Mongo2Go 0.1.0, August 15 2012

  • first alpha version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

How to contribute

Just fork the project, make your changes send us a PR.
You can compile the project with Visual Studio 2017 and/or the .NET Core 2.0 CLI!

In the root folder, just run:

dotnet restore
dotnet build
dotnet test src/Mongo2GoTests

mongo2go's People

Contributors

0xced avatar alexzeitler avatar asimmon avatar avrum avatar bannerflow-hassaan avatar bastianeicher avatar corentinaltepe avatar cottsak avatar dannybies avatar g3n7 avatar johanneshoppe avatar jsweetland-ar avatar kenoma avatar krippz avatar kubal5003 avatar mahisatyanarayana avatar marcpiechura avatar marioleed avatar mjebrahimi avatar onyxmaster avatar realliangshiwei avatar stanko-culaja-kolotree avatar vannevelj avatar vikkol avatar zmira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongo2go's Issues

Option to set the default port for StartForDebugging()

I would like to be able to set a port when running StartForDebugging() as the current default port (27017) may be in use on my local machine which in turn causes my integration tests to fail.

I thought of two options on how to implement this, I'd be happy to create a PR if this is something that you want to add to Mongo2Go.

  • Add an optional parameter port which defaults to MongoDbDefaults.DefaultPort
  • Add an optional parameter searchForAvailablePort which defaults to false. When set to true it will use portPool.GetNextOpenPort() to find the next available open port.

MongoDbRunner.Start() not work correctly in vs 2017 test tool with xunit project

MongoDbRunner.Start() not work in vs 2017 test tool with xunit project in run test operation and debug test operatioin
MongoDbRunner.StartForDebugging() is done with run test operation and debug test operatioin in vs 2017 test tool with xunit project.

MongoDbRunner.Start() and MongoDbRunner.StartForDebugging() all correct in dotnet test command line tool

Mongo2Go.MonogDbBinariesNotFoundException v2

@nickmkk commented here regarding isse #39


Is this supposed to be fixed? I'm still getting this error with v2.2.6 in projects that use PackageReference nuget packages. In my case the actual binaries are here C:\Users\nickm\.nuget\packages\mongo2go\2.2.6\tools\mongodb-win32-x86_64-2008plus-ssl-3.6.1\bin since the PackageReference option just uses packages from the global cache.

Mongo2Go.MonogDbBinariesNotFoundException
Could not find Mongo binaries using the search pattern of "tools\mongodb-win32*\bin". We walked up to root directory from C:\...\bin\Debug and packages\Mongo2Go*\tools\mongodb-win32*\bin.  You can override the search pattern when calling MongoDbRunner.Start.  We have detected the OS as Microsoft Windows 10.0.17134 
   at Mongo2Go.Helper.MongoBinaryLocator.ResolveBinariesDirectory()
   at Mongo2Go.Helper.MongoBinaryLocator.get_Directory()
   at Mongo2Go.MongoDbRunner..ctor(IPortPool portPool, IFileSystem fileSystem, IMongoDbProcessStarter processStarter, IMongoBinaryLocator mongoBin, String dataDirectory)
   at Mongo2Go.MongoDbRunner.Start(String dataDirectory, String searchPatternOverride)

I think "ResolveBinariesDirectory" should look for the binaries in the current users global cache if they are not found. It would solve this issue for people who use PackageReference(which is the nuget standard going forward).

Question: Using Mongo2Go with xUnit and Mock

I am having a hard time setting up the integration tests with Mongo2Go for our app
( which implements Repository pattern) and project requirements to use xUnit (and Mock ).

If anyone is using Mongo2Go with xUnit and Mock, whould you please share some examples?

Error on Linux System.ComponentModel.Win32Exception

Im using Mongo2Go 2.2.8 with Xunit, targeting framework netcoreapp2.1

In Windows everything works fine, but in Linux (Docker Container from microsoft/dotnet:2.1.302-sdk-alpine3.7) when I try to execute "dotnet test" I get the following error "System.ComponentModel.Win32Exception"

First I had some problems with the locate of binaries and I fixed with:

var _runner = MongoDbRunner.Start("MongoData/", "tools/mongodb-linux*/bin", "/root/.nuget/packages/mongo2go/2.2.8");

I checked permissions inside container on folder "MongoData" (generated in /bin/debug/netcoreapp2.1/) and in the folder of binaries, all are root:root. The binaries have the execute bit.

UPDATE:

If I run dotnet test from my linux machine (Centos7), everything works fine, its a problem with container.

Any ideas ?

Stack Trace is:

Failed   SendAsync Test Valid
Error Message: System.ComponentModel.Win32Exception : No such file or directory
Stack Trace:
   at Interop.Sys.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setUser, UInt32 userId, UInt32 groupId, Int32& lpChildPid, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean shouldThrow)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at Mongo2Go.Helper.ProcessControl.StartAndWaitForReady(Process process, Int32 timeoutInSeconds, String processReadyIdentifier, String windowTitle)
   at Mongo2Go.Helper.MongoDbProcessStarter.Start(String binariesDirectory, String dataDirectory, Int32 port, Boolean doNotKill, Boolean singleNodeReplSet)
   at Mongo2Go.MongoDbRunner..ctor(IPortPool portPool, IFileSystem fileSystem, IMongoDbProcessStarter processStarter, IMongoBinaryLocator mongoBin, String dataDirectory, Boolean singleNodeReplSet)
   at Mongo2Go.MongoDbRunner.Start(String dataDirectory, String binariesSearchPatternOverride, String binariesSearchDirectory, Boolean singleNodeReplSet)

Difference in performance between Start and StartForDebugging

Hi,

Thank you very much for this project, it has helped me out a lot!
While creating integration tests I have noticed that there is a significant performance hit when using MongoDbRunner.Start() versus MongoDbRunner.StartForDebugging().

I am running MongoDbRunner.Start() for every single test and I dispose the runner after the test has completed. For 23 integration tests this takes around 1 minute. If I change this to StartForDebugging my tests take around 3 seconds.

I was wondering why there is such a big difference? And if there is something that I can setup to make my tests run faster with .Start().

Thanks!

Multiple processes don't seem to work in parallel

I'm using [assembly: CollectionBehavior(DisableTestParallelization = true)] to ensure that my test processes don't run along side each other. When I remove this switch the processes hang (if there is more than one running at a time) and no tests complete.

I've tested this when running another mongod.exe process running (another local server) and with the [assembly: CollectionBehavior(DisableTestParallelization = true)] tests pass, without, they hang. So I know it's not about the total number of concurrent mongod.exe processes on the system.

If they're using different ports and different folders on disk, why the contention?

Any chance we could update to 3.2.6

I am trying to use $lookup and would love to be able to test using mongo2go. I am going to look at how to update this but if its easy for y'all to do it, I would certainly appreciate it.

mongod.exe remains active after tests complete

I'm using the default net core XUnit project, but targeting net461.

When I run several tests, sometimes mongod.exe instances remain active until I can terminate them.

I shall attempt to put together a small repro when I get time.

P.S. Any reason this is targeting net standard 1.6 instead of an earlier version?

Can't run Mongo2Go on VSTS / Azure Devops

Currently, I’m working with Mongo2Go for unit test. But when I build my Project on VSTS and see it doesn’t work properly. You can check the following error

2018-08-09T09:06:03.9933372Z [xUnit.net 00:00:40.97]     xxxxxxx.BackEnd.Tests.Project.ProjectReadServiceTests.TestGetProjectDetailAsync [FAIL]
2018-08-09T09:06:04.0574498Z Failed   xxxxxxx.BackEnd.Tests.Project.ProjectReadServiceTests.TestGetProjectDetailAsync
2018-08-09T09:06:04.0574987Z Error Message:
2018-08-09T09:06:04.0575333Z  System.AggregateException : One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "2", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 2, EndPoint : "127.0.0.1:27019" }", EndPoint: "127.0.0.1:27019", State: "Disconnected", Type: "Unknown" }] }.)
2018-08-09T09:06:04.0575882Z ---- System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "2", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 2, EndPoint : "127.0.0.1:27019" }", EndPoint: "127.0.0.1:27019", State: "Disconnected", Type: "Unknown" }] }.
2018-08-09T09:06:04.0576249Z Stack Trace:
2018-08-09T09:06:04.0576430Z    at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
2018-08-09T09:06:04.0576638Z    at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
2018-08-09T09:06:04.0576802Z    at System.Threading.Tasks.Task.Wait()
2018-08-09T09:06:04.0577004Z    at xxxxxxx.BackEnd.Tests.Project.ProjectReadServiceTests.TestGetProjectDetailAsync() in D:\a\1\s\Tests\xxxxxxx.BackEnd.Tests\Project\ProjectReadServiceTests.cs:line 116
2018-08-09T09:06:04.0577230Z ----- Inner Stack Trace -----
2018-08-09T09:06:04.0577420Z    at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
2018-08-09T09:06:04.0577636Z    at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
2018-08-09T09:06:04.0577823Z    at MongoDB.Driver.Core.Clusters.Cluster.<WaitForDescriptionChangedAsync>d__50.MoveNext()
2018-08-09T09:06:04.0578017Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0578597Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0578857Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0579088Z    at MongoDB.Driver.Core.Clusters.Cluster.<SelectServerAsync>d__43.MoveNext()
2018-08-09T09:06:04.0579303Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0579791Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0580027Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0580245Z    at MongoDB.Driver.MongoClient.<AreSessionsSupportedAfterSeverSelctionAsync>d__37.MoveNext()
2018-08-09T09:06:04.0580455Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0580694Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0580901Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0581107Z    at MongoDB.Driver.MongoClient.<AreSessionsSupportedAsync>d__34.MoveNext()
2018-08-09T09:06:04.0581326Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0581817Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0582028Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0582227Z    at MongoDB.Driver.MongoClient.<StartImplicitSessionAsync>d__26.MoveNext()
2018-08-09T09:06:04.0582406Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0582762Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0582941Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0583294Z    at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d__91`1.MoveNext()
2018-08-09T09:06:04.0583494Z --- End of stack trace from previous location where exception was thrown ---
2018-08-09T09:06:04.0583670Z    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2018-08-09T09:06:04.0584382Z    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2018-08-09T09:06:04.0584581Z    at MongoDB.Driver.MongoCollectionBase`1.<InsertOneAsync>d__64.MoveNext()
2018-08-09T09:06:04.1605265Z 2018-08-09T09:06:04.159+0000 I CONTROL  [initandlisten] MongoDB starting : pid=3112 port=27020 dbpath=C:\Users\XXX\AppData\Local\Temp\xdol0kvu.gus34704c47a97744dda37b_27020 64-bit host=factoryvm-az49

Add binaries for MongoDB 3.4

It would be great if we could be able to run MongoDB 3.4 (personally I need it since there are new Aggregation framework operators which I am using)

System.ObjectDisposedException : Cannot access a disposed object while running test

Setup for this issue: OSX, dotnet core 2.2, Mongo2Go latest version, NUnit.

First thing, thanks for the work on this! Nice little project you have here and I appreciate having this as an option.

I have a method I'm testing that looks like this:

public async Task<Thing> FindAsync(string id)
{
  var mongoCollection = this.getMongoCollection();
  var results = await mongoCollection.FindAsync(
    thing => thing.Id == id
  );

  if (results.Any())
  {
    return results.First();
  }

  return null;
}

I start up Mongo2Go with NUnit so that it will only be configured once at the beginning of running the test suite and then holding onto references to each of these during the execution of each test:

  private MongoDbRunner _runner;
  private MongoClient _client;
  private IMongoDatabase _fakeDb;

  [OneTimeSetUp]
  public void Init()
  {
    _runner = MongoDbRunner.Start();
    _client = new MongoClient(_runner.ConnectionString);
    _fakeDb = _client.GetDatabase("ThingiesIntegrationTest");

    var sampleThing = new Thing();
    sampleThing.Id = "abc123";
    _fakeDb.GetCollection<Thing>("Thingies")
      .InsertOne(sampleThing);

    var test = _fakeDb.GetCollection<FileSummary>("Thingies")
      .Find(f => f.Id == sampleFileSummary.Id);
  }

The test itself that's exercising the conditional in the code is:

  [Test]
  public async Task FindAsync_Locates_Thing()
  {
    var realThingId = "abc123";
    var thing = await Repository.FindAsync(realThingId);
      
    Assert.That(
      thing.Id, Is.EqualTo(realThingId), "Thing ID's do not match");
  }

When I execute the tests in this setup, I get the following exception:

System.ObjectDisposedException : Cannot access a disposed object.
Object name: 'AsyncCursor`1'.
Stack Trace:
   at MongoDB.Driver.Core.Operations.AsyncCursor`1.ThrowIfDisposed()
   at MongoDB.Driver.Core.Operations.AsyncCursor`1.MoveNext(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorExtensions.GetFirstBatch[TDocument](IAsyncCursor`1 cursor, CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorExtensions.First[TDocument](IAsyncCursor`1 cursor, CancellationToken cancellationToken)
   at Thingies.Repositories.ThingieRepository.FindAsync(String id) in /Users/asdf/Code/project/Repositories/ThingieRepository.cs:line 38
   at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext context)

I'm not exactly sure why the cursor is being disposed of after invoking Any() on it. If I omit that line and re-run this test, it passes as expected. I can also work around this issue by changing to AsyncCursor.FirstOrDefault() for my scenario, but I'd like to know what I'm doing wrong.

I'm glad to dig in and provide more detailed information (or even a PR if it comes to it). I need a pointer on what information I should get and where to begin looking at things. Or if I am just using this wrong, I'll correct that mistake myself.

Feature Compatibility Version

Hello,

I'm using mongo's new "decimal128" type that came in v3.4 and unfortunately I get errors when running my tests with mongo2go. I've confirmed that the errors go away when testing against a db that's using featureCompatibiltyVersion 3.4. Is it possible to change the featureCompatibilityVersion of mongo2go so it will work with 3.4 features?

Thanks,

-Nick

Invalid file path on Linux when using netcore3.0-preview

My project the way I have it right now works perfectly fine locally (Windows) and on Teamcity, both on .NET Core 2.1. If I update to .NET Core 3.0-preview it continues to work locally but on Teamcity it breaks with the following error:

Error Message:
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]  System.AggregateException : One or more errors occurred. (Could not find a part of the path '/in/tests/unit/MyPackage.Tests/bin/Release/netcoreapp3.0/~/.nuget/packages'.) (The following constructor parameters did not have matching fixture data: MongoIntegrationFixture mongoIntegrationFixture)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj] ---- System.IO.DirectoryNotFoundException : Could not find a part of the path '/in/tests/unit/MyPackage.Tests/bin/Release/netcoreapp3.0/~/.nuget/packages'.
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj] ---- The following constructor parameters did not have matching fixture data: MongoIntegrationFixture mongoIntegrationFixture
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj] Stack Trace:
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj] 
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj] ----- Inner Stack Trace #1 (System.IO.DirectoryNotFoundException) -----
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, EnumerationOptions options)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Enumeration.FileSystemEnumerableFactory.UserDirectories(String directory, String expression, EnumerationOptions options)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at System.IO.Directory.GetDirectories(String path, String searchPattern)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.Helper.FolderSearch.FindFolder(String startPath, String searchPattern)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.Helper.FolderSearch.FindFolderUpwards(String startPath, String searchPattern)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.Helper.MongoBinaryLocator.FindBinariesDirectory(IList`1 searchDirectories)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.Helper.MongoBinaryLocator.ResolveBinariesDirectory()
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.Helper.MongoBinaryLocator.get_Directory()
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.MongoDbRunner.MakeMongoBinarysExecutable()
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.MongoDbRunner..ctor(IPortPool portPool, IFileSystem fileSystem, IMongoDbProcessStarter processStarter, IMongoBinaryLocator mongoBin, String dataDirectory, Boolean singleNodeReplSet)
[20:02:01]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]    at Mongo2Go.MongoDbRunner.Start(String dataDirectory, String binariesSearchPatternOverride, String binariesSearchDirectory, Boolean singleNodeReplSet)

I run it with MongoDbRunner.Start(singleNodeReplSet: true);

In an attempt to solve it I have tried publishing a package with the change as suggested in #61 (comment) but that didn't fix it. It gives the following output:

==>Error Message:
[20:45:31]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]       ==> System.AggregateException : One or more errors occurred. (Could not find Mongo binaries using the search patterns "tools/mongodb-linux*/bin", "packages/Mongo2Go*/tools/mongodb-linux*/bin", and "packages/mongo2go/*/tools/mongodb-linux*/bin".  You can override the search pattern and directory when calling MongoDbRunner.Start.  We have detected the OS as Linux 4.14.42-61.37.amzn2.x86_64 #1 SMP Mon May 21 23:43:11 UTC 2018.
[20:45:31]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]       ==>We walked up to root directory from the following locations.
[20:45:31]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]       ==> /in/tests/unit/MyPackage.Tests/bin/Release/netcoreapp3.0
[20:45:31]	[/in/tests/unit/MyPackage.Tests/MyPackage.Tests.csproj]       ==>/root/.nuget/packages) (The following constructor parameters did not have matching fixture data: MongoIntegrationFixture mongoIntegration)

I currently don't have ssh access to our build servers so I can't take a look on the actual machine but figured I'd let you know regardless. Any idea what this might be? As evident from the stacktrace, we're running a linux distro.

Working on windows 8

I'm try to run some integration tests with mongo2go under windows 8 but try allways fail on the dispose method. It creates the database on c:/data without a problem.

Cheers.

Issues/Workarounds for VSTS Test Assemblies task

I'm using VSTS with VS2017 hosted agent and had issue trying to get Mongo2Go to work. I restore my packages using MSBuild's restore.

  • MSBuild /t:restore restores packages to %USERPROFILE%/.nuget by default, which is inaccessible from the directory searcher.
  • Packages are stored in this folder under {Name}\{Version} currently it assumes {Name}*{Version}*

I worked around this by creating a copy files task with the following settings:

Source Folder: $(USERPROFILE)\.nuget\packages\mongo2go\2.1.0\tools
Contents: **
Target Folder: $(System.DefaultWorkingDirectory)\tools

Unsure what best course of action would be. Allowing an absolute path to packages would be one option? Or making it search %USERPROFILE%/.nuget/packages as well. That would perhaps be better?

Possibility to pass port to Start or to StartForDebugging

I am writing integration tests, for which I would like to use Mongo2go (great idea, by the way).

I have MongoDb server installed on my machine. That instance should be for regular usage of the application.

And I am seeing that MongoDbRunner.StartDebugging, instead of returning a brand new instance, is selecting the one that is installed.

After checking the code, at the private constructor of MongoDbrunner, I have verified that indeed

  • It chooses always the default port and there is no way of selecting another
  • If the mongoDb process is running and the port is used, it figures that MongoDb is already running (thus selecting the installed instance)

That is not the desired behavior. I would like:

  • To be able to pass a port
  • If not, that MongoDbRunner chooses a port that isn't already in use.

If you want, I could even try and develop it by myself and do a pull request.

What do you think?

Thanks a lot by the way

mongo2go linux netcore 1.1

Hello,

I have an issue with mongo2go in a linux environment, using the 2.0.0-aplha1 version.
the method MakeFileExecutable() throws an exception :

Invalid File or Directory attributes value
           at System.IO.FileSystemInfo.System.IO.IFileSystemObject.set_Attributes(FileAttributes value)
           at System.IO.UnixFileSystem.SetAttributes(String fullPath, FileAttributes attributes)
           at System.IO.File.SetAttributes(String path, FileAttributes fileAttributes)
        /data/projects/Mongo2Go/src/Mongo2Go/Helper/FileSystem.cs(35,0): at Mongo2Go.Helper.FileSystem.MakeFileExecutable(String path)
        /data/projects/Mongo2Go/src/Mongo2Go/MongoDbRunner.cs(137,0): at Mongo2Go.MongoDbRunner.MakeMongoBinarysExecutable()
        /data/projects/Mongo2Go/src/Mongo2Go/MongoDbRunner.cs(120,0): at Mongo2Go.MongoDbRunner..ctor(IPortPool portPool, IFileSystem fileSystem, IMongoDbProcessStarter processStarter, IMongoBinaryLocator mongoBin, String dataDirectory)
        /data/projects/Mongo2Go/src/Mongo2Go/MongoDbRunner.cs(37,0): at Mongo2Go.MongoDbRunner.Start(String dataDirectory, String searchPatternOverride)

if I comment this line, everything works as excepted.

Thanks

Mongo2Go sometimes not able to pick up data in collection

Using Mongo2Go for mocking the mongo db calls. Below would be the structure am, using. Environment: .Net Core 2.0 / VS 2017

Helper.cs -> Creates the the Mongo2Go runner instance the returns the same (Non single-ton)

In any test class I am getting the runner instance like new Helper<T>().GetRunnerinstance() and using the same instance for all test cases/methods. below would a small sample code as how using Mongo2Go

        this.mockRepository.Setup(x => x.AddDocumentAsync(It.IsAny<ABC>()))
                              .Callback(() => this.Helper.Collection.InsertOneAsync(abc))
                              .Returns(Task.FromResult<ABC>(abc));

in above piece of code, am using MOQ and calling a local Mongo2Go call instead of actual repository call. Then to verify or assert am trying to get the same inserted local data like

        var data = await this.Helper.Collection.Find(new BsonDocument(nameof(ABC.Name), Constants.Name)).ToListAsync();
        var actual = data.Single(); // this line behaves wired

When I run all the test cases either from VS or through command prompt dotnet test the code line data.Single() fails with exception ... empty collection, no elements. The reason is obvious that the mongo Find() call didn't fetch any result through it should have since I have just inserted the same record.

Very strange thing is that, it runs absolutely fine when I debug the test but fails when just run only that test or all test discovered.

Any idea, why is it behaving so? Please help

Getting port on mono fails

Hi we would like to use Mongo2Go in a cross platform environment, but we hit bit of a snag since:
IPEndPoint[] tcpConnInfoArray = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
is not implemented by mono :(

I was thinking of providing a mono compatible implementation of this, how does that sound?

/cheers
Krippz

Custom Bson Serialization

I use custom Bson serialization in my app:

var pack = new ConventionPack {new CamelCaseElementNameConvention()};
ConventionRegistry.Register("camelCaseConvention", pack, t => true);
BsonClassMap.RegisterClassMap<MyDocument>(m =>
{
   m.AutoMap();
   m.SetIdMember(m.GetMemberMap(e => e.MyId));
});

If I register my naming convention (code from above) in integration tests with Mongo2Go, I got error:

System.ArgumentException
An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at MongoDB.Bson.Serialization.BsonClassMap.RegisterClassMap(BsonClassMap classMap)

If I do not register my naming convention, I got error:

System.FormatException
Element '_id' does not match any field or property of class MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)

Is it possible to teach Mongo2Go to work with custom serializations? What do you think from architecture side?

Request for support of different versions of mongod server

Hi,

Currently the Mongo2Go supports only one version of mongod server (ie is 4.0.3). Could we add support for different versions? It would be great if we can support different flavours such as RHEL or Ubuntu, etc...

I would like to high light some information here.
In javascript world, there are mongodb package downloaders that support this
https://www.npmjs.com/package/mongodb-prebuilt
https://www.npmjs.com/package/mongodb-download

These package facilitating downloading of mongodb server version from the "https://fastdl.mongodb.org" based on the target os version.

providing this flexibility would be great in C# world.

If you are okay with the idea, I can also work on getting the feature implemented. (I might need some information and help)

Regards,
Nachiappan

Hardcoded path passed to MongoDbRunner constructor

There's a hardcoded data path used in the MongoDbRunner.StartUnitTest(IPortPool portPool, IFileSystem fileSystem, IMongoDbProcessStarter processStarter, IMongoBinaryLocator mongoBin) method:

return new MongoDbRunner(portPool, fileSystem, processStarter, mongoBin, "C:\\data\\db");

This is an oversight, I'm sure - you can pass that parameter in Start and StartForDebugging methods but it's unavailable in StartUnitTest. This is a bad thing, since some people, including me, need to specify the used data directory.

For those that have met the same problem, as a quick and dirty workaround you can simply use reflection to call the constructor yourself (it's generally a big no-no to be touching private constructors, but this'll get you moving):

_dataDir = @"c:\my\own\path\to\data";
var constructor = typeof(MongoDbRunner).GetConstructor(
            BindingFlags.NonPublic | BindingFlags.Instance, 
            null, 
            new Type[]
            {
                typeof(IPortPool),
                typeof(IFileSystem),
                typeof(IMongoDbProcessStarter),
                typeof(IMongoBinaryLocator),
                typeof(string)
            }, null);

    _runner = (MongoDbRunner)constructor.Invoke(new object[]
        {
            PortPool.GetInstance, new FileSystem(), new MongoDbProcessStarter(), new MongoBinaryLocator(null), _dataDir
        });

Summarized: StartUnitTest could use another parameter to let us define what data directory to use.

Can't run unit tests for Mongo2Go project

I downloaded source code of Mongo2Go, but have trouble with running tests. They use MSpec, I can't discover them with adapter nor in console (dotnet test). There is a file in solution NO_TESTS.txt. Does it mean they cannot be run now?

Can you run these tests on latest build? Any hints how?

Problem with MacOSX

I got a test case that works well on Windows, and with Mono on Linux, but it fails with Mono on MacOSX.
Test framework used is Nunit

        [Test]
        public async Task TestMongo2Go()
        {
            var tmpDir = System.IO.Path.GetTempPath();
            MongoDbRunner _mongodb = MongoDbRunner.Start(tmpDir);
            var _mongoClient = new MongoClient(_mongodb.ConnectionString);
            IMongoDatabase db = _mongoClient.GetDatabase("unittest");
            var role = new Role { Name = "ALL PERMISSIONS", PermissionTypes = new List<string> { "ALL PERMISSIONS", "READ_ALL" } };
            await db.GetCollection<Role>("role").InsertOneAsync(role);
            var user = new User { Name = "unittest", PersonalNumber = "123", RoleIds = new List<ObjectId> { role.Id } };
            await db.GetCollection<User>("user").InsertOneAsync(user);
            var users = await db.GetCollection<User>("user").Find(u => true).ToListAsync();
            Assert.AreEqual(1, users.Count);
        }

Stack trace is:

TestMono.TestMongo2GoUser
System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "8", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 8, EndPoint : "Unspecified/localhost:27024" }", EndPoint: "Unspecified/localhost:27024", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: Connection refused

No firewall and ping localhost works
Any suggestions would be appreciated

process and data not cleaning up when I stop a test

So I'm running my tests in the R# runner and debugging locally. Before my using block completes I'm stopping the test which AFAIK should call the finaliser on the MongoDbRunner instance. Only, I suspect that, we're not getting past this line https://github.com/JohannesHoppe/Mongo2Go/blob/master/src/Mongo2Go/MongoDbRunner.IDisposable.cs#L28 and we should be. The result is that my process stays alive when it should terminate and my data folder is not cleaned up.

If this can be resolved, then perhaps #16 is moot.

Mongo2Go fails in a long-running integration test

I've set up a collection fixture with MongoDb to be able to test a long running migration job that includes inserting transforming data and inserting millions of records into a MongoDb instance. Tested it on a few smaller files and it worked just fine, yesterday, set it up to work on an actual input file and went home.
Today I was greeted by an error message saying:

Message: MongoDB.Driver.MongoConnectionException : An exception occurred while opening a connection to the server.
---- System.Net.Sockets.SocketException : Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:27021

This happened exactly an hour into the test. It looks like for some reason, at that point there was an attempt to start up another instance of MongoDb, which failed? Not sure why that would happen.

I'm using Xunit 2.3.1 and Mongo2Go 2.2.4 and the target framework of my test project is .NET framework 4.6.1.

Instable on Linux

The testcase below is rock solid on windows, but fails almost every time on Linux, making impossible to run CI on https://circleci.com/ or https://travis-ci.com/ or any other Linux based CI service.

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyUnitTest
{
    [TestFixture]
    class TestMongo2go
    {

        [Test]
        public async Task TestStartMany()
        {
            var old_runners = new List<Mongo2Go.MongoDbRunner>();
            Mongo2Go.MongoDbRunner _mongodb;

            for (int i = 0; i < 25; i++)
            {
                string tmpDir = System.IO.Path.GetTempPath();
                _mongodb = Mongo2Go.MongoDbRunner.Start(tmpDir);
                Console.WriteLine("_mongodb.State: " + _mongodb.State);
                bool dbUp = ProbeForMongoDbConnection(_mongodb.ConnectionString, "unittest");
                Console.WriteLine("Connection to " + _mongodb.ConnectionString + " was " + (dbUp ? "successful!" : "NOT successful!"));
                Assert.IsTrue(dbUp, "mongodb is not alive");
                old_runners.Add(_mongodb);
            }

            foreach (Mongo2Go.MongoDbRunner runner in old_runners)
            {
                runner.Dispose();
            }

        }


        private static bool ProbeForMongoDbConnection(string connectionString, string dbName)
        {
            var probeTask =
                    Task.Run(() =>
                    {
                        var isAlive = false;
                        var client = new MongoDB.Driver.MongoClient(connectionString);

                        for (var k = 0; k < 6; k++)
                        {
                            client.GetDatabase(dbName);
                            var server = client.Cluster.Description.Servers.FirstOrDefault();
                            isAlive = (server != null &&
                                   server.HeartbeatException == null &&
                                   server.State == MongoDB.Driver.Core.Servers.ServerState.Connected);
                            if (isAlive)
                            {
                                break;
                            }
                            System.Threading.Thread.Sleep(300);
                        }
                        return isAlive;
                    });
            probeTask.Wait();
            return probeTask.Result;
        }

    }
}

IOException on Dispose() method calling

I'm trying to start Mongo2Go as following:

 MongoDbRunner.Start(dataPath);

It works fine, but throws IOException on callings Dispose() method ("...unable to access local.0 file...").

At the same time MongoDbRunner.StartForDebugging(dataPath) works ok.

Looks like mongo process still running when trying to delete data files in case of .Start() method.

2.1.0 is not compatible with netcoreapp1.1/1.0

I get the following error if I try to add Mongo2Go 2.1.0 to my .Net Core 1.1 project.
2.0.0-alpha1 works though.

Errors in c:\Projects\****\sln\Test\Test.csproj
Package Mongo2Go 2.1.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Mongo2Go 2.1.0 supports: net (.NETFramework,Version=v0.0)
One or more packages are incompatible with .NETCoreApp,Version=v1.1.

I am using the latest SDK.

prevent windows firewall popup

Can we prevent the Windows Firewall popup from appearing?

I'm getting it when I reset the test environment or restart VS, etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.