Ejemplo n.º 1
0
        public void AutoNumberField_Seed()
        {
            var field = new AutoNumberField( );

            field.Name           = "TestAutoNumberField";
            field.AutoNumberSeed = 123;

            var definition = new Definition( );

            definition.Name = "TestAutoNumberDefinition";
            definition.Fields.Add(field.As <Field>( ));
            definition.Save( );

            Entity instance1 = new Entity(definition.Id);

            instance1.Save( );
            int value = instance1.GetField <int>(field);

            Assert.AreEqual(123, value);

            Entity instance2 = new Entity(definition.Id);

            instance2.Save( );
            value = instance2.GetField <int>(field);
            Assert.AreEqual(124, value);

            Entity instance3 = new Entity(definition.Id);

            instance3.Save( );
            value = instance3.GetField <int>(field);
            Assert.AreEqual(125, value);
        }
Ejemplo n.º 2
0
        public void AutoNumberField_Concurrency( )
        {
            var field      = new AutoNumberField( );
            var definition = new Definition( );
            var deletables = new List <Entity>( );

            try
            {
                field.Name = "TestAutoNumberField";

                definition.Name = "TestAutoNumberDefinition";
                definition.Fields.Add(field.As <Field>( ));
                definition.Save( );

                var    values     = new List <int>( );
                object lockObject = new object( );

                var thread1 = new Thread(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var instance = new Entity(definition.Id);
                        instance.Save( );
                        deletables.Add(instance);
                        int value = instance.GetField <int>(field);

                        lock ( lockObject )
                        {
                            values.Add(value);
                        }
                    }
                });

                var thread2 = new Thread(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var instance = new Entity(definition.Id);
                        instance.Save( );
                        deletables.Add(instance);
                        int value = instance.GetField <int>(field);

                        lock ( lockObject )
                        {
                            values.Add(value);
                        }
                    }
                });

                thread1.IsBackground = true;
                thread1.Start( );

                thread2.IsBackground = true;
                thread2.Start( );

                thread1.Join( );
                thread2.Join( );

                Assert.AreEqual(20, values.Count);
                Assert.AreEqual(20, values.Distinct( ).Count( ));

                for (int i = 1; i <= 20; i++)
                {
                    Assert.IsTrue(values.Contains(i));
                }
            }
            finally
            {
                deletables.ForEach(e => e.Delete( ));

                definition.Delete( );
                field.Delete( );
            }
        }