A New Way To Deal With Time In .NET 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this method has a problem and that problem is right here daytime of said that UTC now and why is our problem but it is a problem because it does not allow us to create proper unit tests for the method and that is because of the following so take a look so this is a very simple method that creates a game match right this game match object but the key rule on this method is that you cannot create get matches unless it is awakened right so that's a rule of the method so if it is if the date time that you see now is not a Saturday or not a Sunday right then we just go ahead and draw this in value operation assumption and that's expected behavior and otherwise we'll go ahead and actually return the game match but the problem is that we cannot write a comprehensive and deterministic test suite for this method here uh because it is not always going to be a Saturday a Sunday or any other day right and so if you take a look very quickly at the unit test I have array prepared so let me show you this very quickly I have a couple of unit tests here right so I have a unit test here that tries to verify what happens on the on the weekends we should be able to create a match on the weekend and the other method here is showing that we should be throwing an exception if it is a weekday and so if we go ahead and try to run this methods and let me show you my activity very very briefly so let's go ahead and into our testing pane over here and I'm going to run these tests what's going to happen is that sometimes the one of these test cases is going to pass and some other times another test case is going to pass right so right now as you can see one of these this cases is a is failing right and this is the test case and let me collapse it for a moment this is this case of the weekend because right now it is a weekend and I don't have a good way to tell this unit test that I'm testing this scenario where we are in a weekend right so right now it's a Friday and so if unit test doesn't really know how to test for that right how to set the environment for that right well in the other case the case of the weekend is just passing very happily right but of course if I try to run this test tomorrow or any other data it's not a weekday these test cases are going to change behavior and so this not just complicates the unit test it actually complicates me testing manually this application or this API so let me show you this if I go into my matchmaker.hdp file over here and I'm going to just run my wrong my server right here so I'm going to test the behavior of the API and of course right now it is a Friday and if I try to go ahead and send a request to this endpoint what's going to happen is that it is going to fail right so can't create matches on on weekdays which is fine uh but I would likely have a good way to verify the actual behavior of the method when it is a it is a weekend right verify that everything works properly even manually and it is it's just too hard to do that right now and so if you have been writing code as for as long as myself you may know the right way to fix this right and that and that is my interest in some sort of abstraction that our class can use to represent the time right so we can somehow influence that behavior in the class and so fortunately and let me close this and let me stop my server fortunately the folks in the.net team as part of the preview 4 of that net 8 have already introduced it a brand new abstraction here let me show you uh that abstraction is called the time provider right so the time abstraction time provider and and so this class here I mean it is a fairly simple class and it's something that you usually create in your team to fix this situation uh but now it comes built in in directly into the.net SDK so you can use it right away let me show you how to use it so let's go back over here now I'm going to go back into my class over here so this class here is actually just a factory right so it's a very simple Factory whose purpose is to be able to create these game matches so the first thing I want to do here to fix this is to introduce an instance of that time provider so to do that all we have to do is the following so private read only time provider time provider all right so that's a Time provider and I'm going to just add a Constructor here to inject our instance of the time provider so time provider provider right and so we're going to say this dot time provider equal sign provider right so now we have an instance of the time provider and by having that we can now take advantage of the methods of that class to figure out what is the current time so for that we're going to scroll down here and instead of keep using daytime offset.udc now what we can do is actually do timeprovider.getc now right and so the cool thing about this is that now we are injecting what is actually an after class so this time provider is an abstract class that you can modify as you please in your unit test or even in a kind of a deaf environmental scenario what you want to somehow influence what's the current time and so and let me show you how to do that so now this class depends on time provider so now let's go back into our unit test over here right and where we have this test case but a failing and the other one passing but now we can do the following so the first thing that we're going to do is to Define what's going to be our current time so I'm going to Define a variable here that I'm going to call now equals new date time offset right and this should be a uh the type of data I want to verify here okay so since this is going to be for a weekend what I want to do is just if I hear a date that's actually going to be awakened and in my case that's going to be 2023 let's say this is May May 20 so May 20 is a weekend and the hours doesn't matter so I'm going to say 0 0 okay so now we have the actual date on which we want to run this specific unit test right so now we can do is create our mock time provider right and I'm using the mock Library here and so this is going to be a simulated time provider based on the abstract type provider right and now we have that what we can do is the following okay so now we can say okay so make sure that now whenever somebody calls get UTC now uh it is going to return the date that I'm specifying right here which is always going to be a Saturday I'm actually going to put here a Saturday all right and so with that in place what I can do now is instantiate my game match Factory using that mocked object so now I can say time provider.object and so and with that I am able to run the unit test in the context of that very specific date that I want to test on right and just like I did this I can go ahead and do this a similar thing for the other test case so I'm going to go down here okay and I'm going to do the same I'm going to prepare a date but in this case I want it to be a weekday right so for that I'm going to be using 2023 5 and this is going to be 22 which is actually uh Monday right it's going to be a Monday and so in that case and I'm going to also go ahead and do um time provider dot object right and so to pass in that object over there and this should be good enough to fix these two test cases and one more thing that I may want to test is uh the iCloud creation date of that game match so because if you see here when we create a game match we are assigning the created time as now right uh which we could also not test before because we couldn't tell what's going to be there now uh over here but now we can actually test for that so now we can say not just that the match should not be no in this case but that the match dot create a Time should be now right it should be exactly now because that's the time that we have prepared for this unit tests and so now we have this in place we can go ahead and just build this code and then we're going to go ahead and run the test cases to make sure that these These are passing now so let me go ahead and run to my testing pane here I'm going to run my test cases once again and so if you just give it a one second I wanted to see that these test cases are as expected are now passing right so it's all good now and that is because we are able like I said to influence the timing for the for the factory that we're using here so that's how you can handle that in unit test but of course I mean you also want to handle this properly at the the ad runtime so and the actual application execution so how do you use this time provider in that case so let me go now into Meyer Explorer and then let's go into parentheses over here right and of course this is where we do all the initial startup initialization of all the objects and registrations and all that uh but one thing the work that we're missing here is that time provider instance right because now we're injecting that here right and in order to do this kind of dependency injection of the time provider we need to register an instance of type provider in program.cs which let me bring it over here what I'm going to do is just say the following so I'm going to do build Services dot at Singleton right and what I'm going to add here is just a new actually a Time provider that system instance over here system here represents the actual time of the system and so with that we should be able to make sure that at runtime we use the time of the system while in the tests we can simulate at a very different time of time provider uh so let's let's run this and let's see what we get now so I'm going to go ahead and run over here and I'm going to collapse this for a moment and let me go ahead and run my server here all right and so with the server running there I'm going to go ahead and click on send request and as you see on the right side I mean we still have the same problem right uh same problem in the application at least loads but we can't create matches on weekdays and I suspected right it's not it's still not not a weekend so how we can actually verify the other case manually if it wanted to to do that what we can do is to do the following so let's go ahead and stop this for a second and let's go back into our program.ts and what I can do is to do something depending on if I am in the development environment or if I am in the a production environment right so instead of doing this what I'm going to do the following so I'm going to close this and I'm going to say the following so I'm going to pass in here my service provider right and so I'm going to be a little bit more explicit on exactly what I want to do here depending on the current environment okay and yeah I think I'm missing something here and there and so in this case what I want to do is the following first I'm going to Define here what is going to be a weekend so so a Saturday and that Saturday I actually defined in my test so I'm going to bring back my Saturday date over here right so it's going to be this that right there and I'm actually not going to name this one as hey Saturday so this is a Saturday and then I'm going to say the following so return Builder Dot environment dot is development so if it is development environment then in that case I'm going to go ahead and return a new time provider but it's not going to be just a generic time provider this is going to be a time provider that I specifically prepared for development purposes right so if you go back here into my Explorer notice that in providers I have created a brand new what I call the devtime provider right so this is the one that we can use for development purposes and all it's doing is just taking the the time of set the UTC now that has been provided at the Constructor and is returning that one as the UTC now that is reflected right here right so because of this I can actually do whatever I want for development purposes at boot time if it is a development environment right so if I go back into product now I can do new Dev time provider right and then this is going to be a a Saturday now for this one I do have to use a one morning space here which is going to be using Matchmaker API that providers right so that's a provider so for the online purposes I'm going to be using that one and otherwise I'm going to be using the other one so I'm going to just be using pen provider dot system and so let's close column there and then that should be good enough for us so if you're in development environment we use our customizage time provider development time provider otherwise we just use the system one so if I now go back and try to run my application right so.net run and then let's go back into our patchmaker HTTP I'm going to go ahead and send requests and this time as you can see here uh we were able to actually create our game match we do have our 200 okay right there and then we have again match where we are waiting for an appointment so this worked just fine and that is because uh we are able to you know mock the time provider and so there you go uh the brand new time provider a small Edition but a fantastic addition to the net SDK so it's going to be included in.net8 and I think it's going to make our lives much much easier and so yeah hopefully you like this video and if you liked it please check out this other video we recovered another topic another topic that I think is you're going to find interesting thanks for watching and I'll see you next time
Info
Channel: Julio Casal
Views: 2,059
Rating: undefined out of 5
Keywords:
Id: qBwk9e8Fkvw
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Tue May 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.